Can I override onbeforeunload for a specific item?

I have a page that does quite a bit of work, and I do not want the user to be able to navigate from this page (close the browser, return button, etc.) without receiving a warning. I found that the onbeforeunload event (which I find is specific to IE, which works fine for me since the project uses a lot of ActiveX) works fine.

The problem is that I want the user to be able to click on the small β€œhelp” icon in the upper right corner and pop up the help window at any time. This calls onbeforeunload to start, even if the main window never goes anywhere and the page is never unloaded.

The JavaScript function that fires when the onbeforeunload event is fired simply puts the text in event.returnValue. If I could somehow make sure that the help icon is the one that was clicked, then I could just not put the text in event.returnValue in this situation. But how can I get this page?

+4
source share
4 answers

Let me guess: the help icon is actually a link with javascript: url:? Change it to a real button, a real link, or at least put the functionality in the onclick event handler (which prevents the default behavior). The problem is resolved.

 <!-- clicking this link will do nothing. No onbeforeunload handler triggered. Nothing. And you could put something in before the return false bit... ...and the onunload handler would still not get called... --> <a href="http://www.google.com/" onclick="return false;">blah1</a> <!-- this should also do nothing, but IE will trigger the onbeforeunload handler --> <a href="javascript:void(0)">blah2</a> 
+8
source

EDIT: My "workaround" below is a complete bust based on my misunderstanding. Go with the answer of Shog9 above.

Ok, so when I wrote the question, I came up with a workaround that will work for now.

I put a global JavaScript variable in action as a boolean regarding whether the icon is in the know. Then I attach the events to the onmouseover and onmouseout images to the events and write the functions that will set this value. Finally, I just have the code in a function that processes onbeforeunload, which will check this value before setting event.returnValue.

This is probably not a perfect workaround, but it will work for now.

+2
source

on the internet you will find many people suggesting using something like

window.onbeforeunload = null

but this does not work for me in IE6. reading in the MSDN docs for the event object, I found a link to the event.cancelBubble property, which I thought was the solution. but thanks to Orso, who pointed out that setting "event.cancelBubble = true" is useless, the way to get rid of the confirmation request is to completely exclude the return statement, I decided to use a boolean variable as a flag to decide whether to return something or not. in the example below, I am adding javascript programtic code in the code behind:

 Page.ClientScript.RegisterStartupScript(typeof(String), "ConfirmClose", @" <script> window.onbeforeunload = confirmExit; function confirmExit() { if(postback == false) return ""Please don't leave this page without clicking the 'Save Changes' or 'Discard Changes' buttons.""; } </script>"); 

then the help button contains the following aspx markup:

 OnClientClick="postback=true;return true; 

this sets the postbackback variable to true, which receives the confirmExit () function, which has the effect of canceling the event.

hope you find this helpful. It is tested and works in IE6 and FF 1.5.0.2.

0
source

I have a method that is a bit awkward, but it will work in most cases.

Create a Holding popup containing a FRAMESET with one 100% FRAME, and place the usual onUnload and onbeforeUnload event handlers in HEAD.

 <html> <head> <script language="Javascript" type="text/javascript"> window.onbeforeunload = exitCheck; window.onunload = onCloseDoSomething; function onCloseDoSomething() { alert("This is executed at unload"); } function exitCheck(evt) { return "Any string here."} </script> </head> <frameset rows="100%"> <FRAME name="main" src="http://www.yourDomain.com/yourActualPage.aspx"> </frameset> <body> </body> </html> 

Using this method, you can use the actual page you want to view, send back and click the hyperlinks without the external onUnload or onbeforeUnload external frame that will be launched.

If the external frame is updated or actually closed, events will fire.

As I said, this is not complete proof, but I will manage to shoot at the event on every click or reverse gear.

-1
source

Source: https://habr.com/ru/post/1276656/


All Articles