简体   繁体   English

使用母版页时卸载事件

[英]unload event when using master page

In my project i have used master page.In one particular page , i want a function to be executed on page unload(javascript event) event of that particular page.To achieve this i have written 在我的项目中,我使用了母版页。在一个特定的页面中,我希望在该特定页面的页面卸载(javascript事件)事件上执行一个函数。为此我已经写好了

    $('body').bind('unload',function()  
{  
alert('hello');  
} );     

But this is not working.This function is not getting called when i move to other page. 但这不起作用。当我移动到其他页面时,这个功能没有被调用。 How should i achieve this. 我该怎么做到这一点。

Well, I suppose its a problem when writing the question but your code should be: 好吧,我认为在编写问题时它有问题,但你的代码应该是:

 $(window).bind('unload',function()
 {
      alert('hello');
 });

You are missing the ending ); 你错过了结局); and the event should be bound to the window... 并且该事件应该绑定到窗口...

[Edit: Added the bind to the window instead of 'body' ] [编辑:添加绑定到窗口而不是'body' ]

$(window).bind("unload", function(){
alert(123);
});

worked for me :) 为我工作:)

$(document).ready(function(){

    $(window).unload( function () {
       alert("Bye now!"); 
    } );

});

try this. 尝试这个。 the document.ready function runs on pageload. document.ready函数在pageload上运行。 so your bind will execute and is should work. 所以你的绑定将执行,应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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