简体   繁体   中英

Is is possible to redirect a page to another url when i click on to the close tab or close a browser window?

I am trying to redirect a page to another url when a user tries to close a browser window.

Can it be possible to redirect without showing any message or popup?

Thanks in advance

It is very much possible by handling "onbeforeclose" and/or "onclose" JavaScript events, but it's not a good practice as the end user does not like to be redirected to a new page upon closing the browser window.

If you want to implement it, you will just have to handle these events and inside the event handler, run the custom code...

You could do something along these lines:

<script language="JavaScript">
    window.onbeforeunload = confirmExit;
    function confirmExit() {
        window.open("http://www.google.com","GoogleWindow");
        return "Do you want to go to google?";
    }
</script>

As mentioned by wanjarisushil , this is not a good method.

window.onbeforeunload = function(){
        window.open("http://www.google.com","newwindow");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM