[英]Is this a good code to redirect parent window to a url instead of the popup and close the popup?
这是示例代码:1.在弹出窗口中:
<body onunload='stopThisAndChangeParentInstead()' >
<a href='MY_URL'> Click here </a>
当用户单击“单击此处”时,将触发卸载事件,该事件将更改父级的URL并关闭此弹出窗口。
function stopThisAndChangeParentInstead()
{
window.opener.top.location.href = "MY_URL";
window.close();
}
我的问题:
我建议在弹出窗口内部的链接上使用target
属性,以使该网址在父窗口中打开( target="_parent"
或target="_top"
应该起作用),然后仅使用JavaScript关闭弹出窗口。 我认为这是一种更清洁的解决方案。
除了建议使用unload事件外,我建议直接将onclick事件附加到锚标记。 使用jQuery,这将是:
$('a').click(function(){
window.opener.top.location.href = $(this).attr('href');
window.close();
return false;
});
尽管这是一种以这种方式设置导航的不寻常方式,但我看不到由此引起的任何问题(除了使用户感到困惑之外)
。
<a id="myLink" href="http://myurl">
...
function stopThisAndChangeParentInstead()
{
var url = document.getElementById('myLink').href;
window.opener.top.location.href = url;
window.close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.