Jump to content

error with onunload event


y

Recommended Posts

hello :)

 

i'v got a problem with th onunload event testing script

it must fires only with the window onunload

but it fires with load either .... why ?

there's somthing wrong i don't know what it is ...

please help

 

here is th html code

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function hello(){alert("welcome to my page");}
</script>
<script type="application/javascript" src="script.js"></script>
</head>
<body onload="hello()" >
</body>
</html>

 

and here's the script code

 


window.onunload = goodbye();
function goodbye(){
alert("Thank you for visiting.come back soon!");
}
//window.addEventListener('unload',goodbye(),false);

i've tried the inline calling with body tag , the window.onunload calling and the lestiner calling

nothing works right

 

please help coz i'm a new :)

thnx

Link to comment
Share on other sites

@Grumpy Old Man

yes i get the "Thank you for visiting. come back soon!" msg when the page first loads

 

@Advanced Member

thanks , finaly i got the idea ..... i didn't know the defference between goodbye; and goodbye();

but calling the function it self didn't work .... no msg appears when unloading the page at all ...!

 

any suggitions ?

 

please help :(

Edited by y
Link to comment
Share on other sites

So you tried changing this:

 

window.onunload = goodbye();

 

to this?

 

window.onunload = goodbye;

 

notice the lack of () after the goodbye.  

 

p.s. - my username is .josh and the "advanced member's" username is nogray.  Our usernames are in the light blue header of each post, same as where it shows your name!

Link to comment
Share on other sites

hmm okay, well here are some possible reasons it's not working:

 

- you are using a browser that doesn't have .onunload (it is not a standard)

- I noticed specifically that the function is being called on unload, but alert is being blocked in my current version of FF and Chrome.

 

You could try using .onbeforeunload instead...

 

window.onbeforeunload = goodbye;

Link to comment
Share on other sites

See my previous post listing the reasons you may still not see it... the bottom line is that these functions are not part of the standard js specification, and even if they are built into a browser, different browsers may or may not block it, or certain things within it.  

 

Add something else in your function like a simple console.log("goobye!").  Open up your javascript console in Chrome.  Refresh the page. You will see that your goodbye function is being called.  You will see that the console.log call works. And you will see a message in the console telling you that Chrome specifically blocked the alert call. 

 

There isn't really anything you can do about this stuff.  The bottom line is that when it comes to performing an action before the page is unloaded, there is no reliable cross-browser method.  And even for what does work, it is limited, because people do not like it when sites do stupid things like popup "are you sure you want to leave?" messages and the like.  Even if you have legitimate operations to perform before a visitor leaves (eg: deleting a logged in session cookie, etc..), the "annoying popup" sites kinda ruined it for everybody.  

 

So basically what I'm advising here is to just forget about coding something for the unload event.  There are other ways to go about whatever it is you need to do, and if what you "need to do" is display some annoying "don't leave!" message, absolutely nobody likes that sort of thing, and it's a guaranteed way to make sure people never return to your site. 

Link to comment
Share on other sites

There's no reason to pop-up an alert saying something like "thank you for visiting". As Josh said that's just going to annoy your users. Browsers have detection now to make sure that kind of thing doesn't happen; calls to alert() and window.open() for example are just ignored, although the event is still fired.

 

What you should be aware of is that trying to add in that kind of thing to the unload event, even if the result is hidden across some browsers, means that most browsers will not be caching the page. At some point it was decided amongst the browser makers that it was too difficult to predict how the load event should be handled when an unload event is fired, so they simply don't cache the page. To the best of my knowledge that's still the case.

 

So basically, not only is it horrific for the user's experience but you're also impacting the performance of your website.

 

It is possible though to add a confirmation box working relatively well across browsers using the beforeunload event.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.