javascript - 如果更改 url 失败,如何让 Chrome 抛出异常
问题描述
我有一个用 js 打开的新窗口的参考
var theNewTab="";
theNewTab = window.open(theURL, 'winRef');
然后我在用户单击父窗口中的另一个链接时使用
theNewTab.location.href = targetLink;
theNewTab.focus();
如果窗口不再存在“关闭”取消链接 FF 和 IE,我使用 chrome 的 id 不会引发异常,我用它来再次打开窗口。
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>
这个例子对你有帮助吗?
推荐阅读
- java - 地精错误:- java.io.IOException:java.lang.ClassNotFoundException:
- video - Sendbird 视频聊天正在回响
- html - 分词不遵循文本对齐
- java - 使用 Spring Reactive (R2DBC) 连接 MSSQL,创建 bean 时出错
- python - Python线程定时器不填充输出文件
- javascript - 如何使用 React 在电子学习网站中为每个课程选项创建课程页面?
- flutter - 覆盖 TextFormField 的粘贴事件
- php - DATE(FROM_UNIXTIME( <
>)) 和 TIME(FROM_UNIXTIME( < >)) - c++ - 带有 MinGW 的 Dev-C++ 5.11 有链接问题
- javascript - 尝试重定向时出现“错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头”