• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Stopping the onbeforeunload event

 
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a onbeforeunload called out in the body tag so the user is warned not to close the page without saving.

Within the page I have input boxes that require user input. Some of the inputs have a link that opens another window with options they can choose. I need to disable the onbeforeunload event from displaying the message on these occations. Here is my JS code but it does not prevent message.

 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
onbeforeunload is firing for that?

Eric
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Eric
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have made changes to my code to reflect what you suggested(I think) but the function called from the hyperlink still produces the warning. In the body tag of my HTML I have:

onbeforeunload = "testbul(true)"

In my function that opens another window I have:



Note: the above function is called from a hyperlink:

javascript:setModel

I just do a test to replace the hyperlink with a general button and it works great so it must be the hyperlink is firing the onbeforeunload before the function setModel is fired. Any way around this?

My testbul function is:



[ November 20, 2008: Message edited by: Steve Dyke ]
[ November 20, 2008: Message edited by: Steve Dyke ]
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what does your hyperlink look like? If it is firing it, are you canceling its default action?

Eric
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my hyperlink

 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You really should not use javascript in the href. Bad practice for when JS is disabled

The link is still doing its default action and you not canceling it.


better is

Eric
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Eric Pascarello:
You really should not use javascript in the href. Bad practice for when JS is disabled

The link is still doing its default action and you not canceling it.

better is

Eric



I do a lot of jQuery calls. The hyperlink looks so much neater that buttons. So I use the JS in my links. How could I get the calls to the jQuery functions with out the JS in the link?
[ November 20, 2008: Message edited by: Steve Dyke ]
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Eric Pascarello:
You really should not use javascript in the href. Bad practice for when JS is disabled

The link is still doing its default action and you not canceling it.



Eric




I get a page error:

'return' statement outside of function
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoops that would be void(0) and not return false for that one.

The reason is it bad to have the javascript in the url is do a "Ctrl + Click" on the link and see what happens.

Eric
 
Steve Dyke
Ranch Hand
Posts: 2206
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even with void(0) in the A tag, the onbeforeunload event still fires before the JS code can set a check variable value.

When the link(javascript:myfunction) is clicked

onbeforeunload event is fired

JS function myfunction is called

variable value is set that is supposed to cause onbeforeunload event to skip the returnValue condition.
[ November 20, 2008: Message edited by: Steve Dyke ]
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This runs fine for me:



Eric
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic