onbeforeunload

onbeforeunload与onunload事件

会有一股神秘感。 提交于 2019-11-27 14:13:37
Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过 window.onunload来指定或者在<body>里指定。区别在于onbeforeunload在onunload之前执行,它还可 以阻止onunload的执行。   Onbeforeunload也是在页面刷新或关闭时调用,Onbeforeunload是正要去服务器读 取新的页面时调用,此时还没开始读取;而onunload则已经从服务器上读到了需要加载的新的页面,在即将替换掉当前页面时调用。Onunload是无 法阻止页面的更新和关闭的。而 Onbeforeunload 可以做到。 1、onbeforeunload事件:   说明:目前三大主流浏览器中firefox和IE都支持onbeforeunload事件,opera尚未支持。 用法: ·object.onbeforeunload = handler ·<element onbeforeunload = “handler” … ></element> 描述: 事件触发的时候弹出一个有确定和取消的对话框,确定则离开页面,取消则继续待在本页。handler可以设一个返回值作为该对话框的显示文本。   触发于:    ·关闭浏览器窗口    ·通过地址栏或收藏夹前往其他页面的时候    ·点击返回,前进,刷新,主页其中一个的时候

Does onbeforeunload event trigger for popup.html in a google chrome extension?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 12:49:34
I'm writing a google chrome extension with a popup and a background page. The popup subscribes to certain events that the background generates, and I would like to unsubscribe from those events when the popup goes away. However, I don't see either of onbeforeunload or onunload events being generated from the popup. Are these events fired? If not, any ideas on how to capture popup close? Pauan "beforeunload" is not fired for browser action popups. Believe me, I tried. "unload" should be fired correctly, however, so listen for that. Actually, I have a similar system in place, that allows you to

beforeunload Or onbeforeunload

丶灬走出姿态 提交于 2019-11-27 12:16:23
问题 I'm stuck working out which one of these I should be using: beforeunload or onbeforeunload They both seem to be doing very similar things, but with different browser compatibility. Some context : I have a form. On page load I serialise the form and save it in a variable. If the user leaves the page I serialise the form and compare the two, to see if there's been any changes. However, if the form is submitted then the event should not be fired. Example 1 I have this working as expected. I just

onbeforeunload confirmation screen customization

限于喜欢 提交于 2019-11-27 09:43:53
Is it possible to create a custom confirmation box for the onbeforeunload event in a browser? I tried but then I get 2 confirmation boxes (one from me which is nothing more than return confirm... and then the standard one from the browser). At the moment my code looks like: var inputChanged = false; $(window).load(function() { window.onbeforeunload = navigateAway; $(':input').bind('change', function() { inputChanged = true; }); }); function navigateAway(){ if(inputChanged){ return 'Are you sure you want to navigate away?'; } } I'm using jQuery for this. window.onbeforeunload = function (e) {

Send AJAX to server beforeunload [duplicate]

匆匆过客 提交于 2019-11-27 09:07:16
This question already has an answer here: JavaScript, browsers, window close - send an AJAX request or run a script on window closing 7 answers So supposedly starting at Firefox > 4 , binding the window jQuery object to beforeunload doesn't work anymore. What'd I'd like to do is submit an AJAX post to delete my server's memcache data. When I refresh the only open tab, I can see that the beforeunload event is called in both firefox and chrome with the following code as evidenced by the console.log message, "firefox/NON-firefox delete". The problem is that I never see the console.log message

JavaScript + onbeforeunload

馋奶兔 提交于 2019-11-27 09:01:37
I have a query regarding my application. Whenever user closes browser window accidentally I'd want to do some clean up operations before that. I have used onunload event but problem is this event is sometimes is firing and sometimes doesn't. What should I do, is there better way to handle such type of problem. rahul window.onbeforeunload = function() { return 'You have unsaved changes!'; } See the MSDN article on onbeforeunload Also there is a similar question in SO Davide Ungari Try: window.onbeforeunload = function() { ...your code... } From my experience onunload works differently in

Is there any way to make onbeforeunload work in Opera?

╄→гoц情女王★ 提交于 2019-11-27 08:49:49
问题 I know, that Opera doesnt support JS onbeforeunload message and will not support it. But is there any way to make it work somehow? Or maybe some jQuery plugins can make it work and let script know if window is going to close? I found only way to make onclick for every a tag, but this is not the complete solution. 回答1: Upgrade to Opera 15. onbeforeunload is now supported in that version because they moved to the WebKit engine. It is still not supported in 12.x because that version and all

Reload the site when reached via browsers back button

泄露秘密 提交于 2019-11-27 07:38:14
Problem: I have a site with dynamic content which needs to be reloaded every time the user sees it. This includes the use case when a user hits the back button on an another site and comes to the site needed to be reloaded. Most (all?) browsers don't refresh the site after this event. My solution (which isn't quite working): http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript window.onbeforeunload = function () { // This function does nothing. It won't spawn a confirmation dialog // But it will ensure that the page is not cached by the browser. } But it still doesn't refresh

Why is jQuery onbeforeunload not working in Chrome and Firefox? [duplicate]

ぐ巨炮叔叔 提交于 2019-11-27 06:42:09
问题 This question already has an answer here: Crossbrowser onbeforeunload? 5 answers jQuery onbeforeunload is not working in Chrome and Firefox. It works properly in IE and Safari. jQuery(window).bind('onbeforeunload' ,function () { mymethod(); }); Above code works properly in IE and in Safari but not in Firefox and Chrome. 回答1: According to MDN's window.onbeforeunload reference, The function should assign a string value to the returnValue property of the Event object and return the same string.

Is there an alternative method to use onbeforeunload in mobile safari?

泪湿孤枕 提交于 2019-11-27 04:21:43
Is there an alternative method to use instead of onbeforeunload in mobile-safari? I've noticed that Google was able to capture the onbeforeunload event in mobile-safari. Has anyone figured out how they are doing so? Google was able to do so using their gmail client. Create a new message... type something in the text area... hit the browser back button. It pops up a warning message. I used an iPad to do my test. I would guess that they are using the History API. By listening to popstate window.addEventListener("popstate", function(e) { ... }); you can show a warning before allowing doing