Home >Web Front-end >JS Tutorial >Solution to window.onbeforeunload method not working properly under IE_javascript skills

Solution to window.onbeforeunload method not working properly under IE_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:35:571080browse

The cause of the incident was that customers reported at work that users often ordered products while browsing the web, but because they opened too many windows at once, or they mistakenly pressed the F5 key when typing on the keyboard. , causing the page to refresh or close abnormally, and at this time all the information on the operations done on the web page is lost. It would be great if we could provide a prompt when the customer information is not processed. The following code It can detect the message that the user is about to leave regardless of whether the user clicks to close, or closes on the taskbar, clicks back, refresh, or presses the F5 key.

Copy code The code is as follows:



You only need to register the bindunbeforunload() method on the page to be detected. You can register this method in the body's onload or document.ready. Here we use What is window.onbeforeunload is that a prompt box will pop up before the page is about to be unloaded. Okay, let’s test it now. Test code:
Copy code The code is as follows:


this is id onbeforunload event test



test is start





The above code That’s all the code I tested this time. Now refresh the page. Well, good, the dialog box we expected pops up. But when I opened the above code under IE, my "Delete Event Binding Button" did not work, which made me very depressed. Two-thirds of the domestic users are using IE, especially when using the damn IE6, 7. If my code cannot work normally under IE6 and 7, it means that my work is in vain. Of course, there is no need to think about bonuses. The methods are all thought up by people. Well, I use a global variable to control whether the dialog box pops up. The modified "javascript" code is as follows:
Copy code The code is as follows:



When the delete binding event is called at the same time, change the value of the variable goodexit to: true means that the user exited normally, so the dialog box will not pop up.
Test again, it’s normal, yes, this is the result I want!
This article is from Brandon Himes, and it is also from a Google snapshot. The source link can no longer be found. I originally wanted to translate it, but my level is really limited and I can only understand it. If the translation is not good, On the contrary, it invites scolding. If friends want to read the source text, please click here directly. This is the snapshot address and may not exist after a while.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn