javascript help needed
I am using onbeforeunload function to alert the user before he closes his browser window. Currently the alert will be called if we close any tab of the browser.
I just need this functionality to be:
1) Alert befor开发者_运维技巧e only when our tab is trying to close, ie this alert will not show when we try to close other tabs.
2) Should show when we try to close the whole browser using close button top right-end corner..
Or with the help of jquery can we attain it?
you can place the alert onunload event of body tag of your site pages.
<body onunload="alert('Your site is about to close');"/>
I am not sure what browser you are using, but I've tried this on IE and Chrome and so far the behaviour is exactly what you described (onbeforeunload is called when the actual tab is closing, when the browse close button is pressed, but does not get invoked when other tab is closing.)
And the following code will do it in jquery...
$(function() {
$(window).bind("beforeunload", function(e) {
return "Are you sure?"
});
});
right the code in the unload event of the document.
document.onunload = UnloadCode;
function UnloadCode() { alert(); }
you could user jquery:
$(window).unload( function () { "alert('Message alert');" } );
<body onbeforeunload="closeWindow"/>
<script>
function closeWindow(){
return 'Your are Realy close window?';
}
</script>
精彩评论