Redirect popup on main window close

霸气de小男生 提交于 2019-12-10 12:05:32

问题


A pop up is opened when a user clicks a button. This pop up will go to a holding page, and wait for the user to leave the website or close the browser.

Then when the user leaves or closes the browser, it will redirect the pop up location to a different site.

I have tried several variants of the following code:

win=window.open('google.com','popup'); //just to illustrate the "win" = my window.open().

$(window).bind('beforeunload', function() {
   window.win.location.href="http://the-new-location.com";
   //tried something like this as well:
   //win.location.href="http://the-new-location.com";
});

But without luck. I am not brilliant with javascript /jquery so any help on how to make this work would be deeply appreciated.

Thanks!


回答1:


I know you already have a prototype working, but if you want the jquery option:

var popup = window.open('http://google.com', 'popup');

$(window).unload(function() {
    if(!popup.closed) {
        popup.location.href = 'http://surveyurl.com/';
    }
});

You should also be checking to see if popup.closed is there.




回答2:


I found a solution:

win=window.open('google.com','popup');

window.onunload = redirect;
function redirect(){
   window.win.location.href="http://the-new-url.com";
};


来源:https://stackoverflow.com/questions/10089034/redirect-popup-on-main-window-close

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