开发者

Show popup when the browser closes

popup when the browser closes

I am using onbeforeunload event

<script>
function showPopup()
{

  urlstring = "http://www.mydomain.com/popup.php";
            window.open(urlstring,'mywin',"height=400px,width=500px,status=no,toolbar=no");

}



</script> 

<body onbeforeunload="showPopup(); ">

but it also show popup when ever I hit back space and page refresh.

I want show popup only when browser close and not show when hit back space.

but it shows all conditions.

Please suggest me any other sol开发者_如何学Cution for this.


This is showing a popup every time because the onbeforeunload event is not aware of the domain you are leaving, it is only aware of the current page unloading for a new one.


Well , here's the bitter truth...

onbeforeunload is fired on page refresh and browser close.

The onbeforeunload event is fired every time the page is about to unload.

Which includes

  1. Clicking on a link
  2. Submitting a form
  3. Closing the browser(or tab)
  4. Refreshing the page

You might find this helpful http://www.webdeveloper.com/forum/archive/index.php/t-36808.html


    <script>
    function showPopup(){
        urlstring = "http://www.mydomain.com/popup.php";
        window.open(urlstring,'mywin',"height=400px,width=500px,status=no,toolbar=no");
    }

    window.onunload= showPopup();
    </script> 

Note: This is DOM Level 0. Not part of any standard

EDITED: Note Infotekka's link to the document.onunload will also work appropriately for this problem.


You can't.

Your page, including its events, is not aware of other tabs, other pages or the browser itself for that matter. It also doesn't know why it is unloaded. The onunload event is the closest you will get, but it fired everytime when your page is unloaded, so also on navigation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜