我有一个用js打开的新窗口的引用
var theNewTab="";
theNewTab = window.open(theURL, 'winRef');
然后,当用户单击父窗口中的另一个链接时,我使用
更改 中的 urltheNewTab.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/