Is it possible for onunload confirm to open a new url in the same window?

南楼画角 提交于 2019-12-12 04:09:30

问题


this is my first post and I'm a JS novice, so please forgive my ignorance...

Heres my question, its in two parts:

1.) At onunload I want an alert that asks the user if they would like to go to a related URL. The code I'm using works, but it open the URL in a new window and this can be blocked by a pop-up blocker even though the user has opted-in. Is there a way to have it open in the same window and negate the pop-up blocker?

2.) is there a way to take the onunload function out of the body tag and put it the script?

Heres the code I'm using:

<script language=javascript>
function confirmit()
{
var closeit= confirm("Before you go would you like to add your press kit to the Search Press Kits database?");
if (closeit == true)
{window.open("http://NEWURLHERE.com");}

else
{window.close();}
}
</script> 

</head>

<body onunload="confirmit();"> 
peace
</body>

Thanks in advance,

Dan


回答1:


  • If there would be a way to bypass the pop-up blocker it would be a bad pop-up blocker.
  • As far as I know you need a popup, because when onUnload is executed, the original window might be already closed.
  • Yes, you can move it to the script part:

    function confirmit() { var closeit= confirm("Before you go would you like to add your press kit to the Search Press Kits database?"); if (closeit == true) {window.open("http://NEWURLHERE.com");} else {window.close();} } window.onunload = confirmit


来源:https://stackoverflow.com/questions/2813455/is-it-possible-for-onunload-confirm-to-open-a-new-url-in-the-same-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!