javascript - 如何让 Chrome 在更改 url 失败时抛出异常

标签 javascript google-chrome exception

我有一个用js打开的新窗口的引用

var theNewTab="";
theNewTab = window.open(theURL, 'winRef');

然后,当用户单击父窗口中的另一个链接时,我使用

更改 中的 url
theNewTab.location.href = targetLink;
theNewTab.focus();

我在使用 chrome 时遇到的问题是,如果窗口不再“关闭”,则 id 不会抛出异常,取消链接 FF 和 IE,我用它来再次打开窗口。

try {
   theNewTab.location.href = targetLink;
   theNewTab.focus();
}catch(err) {
   theNewTab = window.open(theURL, 'winRef');
   theNewTab.focus();
}
PS:我每次都尝试使用“window.open”,但如果窗口已经打开,id不会重新加载页面,或者它会重新加载页面,但它不会重新执行我认为已准备好文档的脚本。

最佳答案

我不确定你需要什么。

<script type="text/javascript">
  var theNewTab = null;

  function openNewTab(theURL) {
    if (theNewTab == null || theNewTab.closed == true) {
      theNewTab = window.open(theURL);
    } else {
      theNewTab.location.href = theURL;
    }
    theNewTab.focus();
  };

  // use the function when you need it
  $('a').click(function() {
    openNewTab($(this).attr('href'));
  });
</script>

这个例子对您有帮助吗?

关于javascript - 如何让 Chrome 在更改 url 失败时抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50322082/

相关文章:

hibernate - 无法配置 Jasypt + Hibernate

c# - 以 json 格式而不是纯 html 从 aspx 页面获取响应

javascript - 我在循环中忘记了什么?

javascript覆盖新标签页chrome

ruby - 在 Ruby 中拯救 bash/系统 STDERR

c# - 如何使用 RestSharp 捕获异常

javascript - 捕获VLC Webplugin的MediaPlayerPlaying事件

javascript - JustGage Javascript 值中的 PHP 数据

javascript - 在 iframe 中放置通用菜单以从单个位置加载的任何通用方法

javascript - 无法从 Javascript 获取返回值