Jump to content


Photo

error with onunload event

onunload

  • Please log in to reply
10 replies to this topic

#1 y

y

    Member

  • Members
  • PipPip
  • 15 posts

Posted 27 January 2013 - 10:00 AM

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

#2 .josh

.josh

    .josh

  • Administrators
  • 14,745 posts

Posted 27 January 2013 - 02:49 PM

so are you saying you get that "Thank you for visiting. come back soon!" message when your page first loads?

Did I help you? Feeling generous? Donate to me! || Donate to phpfreaks!
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 nogray

nogray

    Advanced Member

  • Members
  • PipPipPip
  • 930 posts
  • LocationSan Francisco CA

Posted 27 January 2013 - 04:46 PM

You are assigning the output of the function goodbye to your onunload event. You need to assign the function itself instead,
e.g.
window.onunload = goodbye;

  • y likes this

NoGray.com


#4 y

y

    Member

  • Members
  • PipPip
  • 15 posts

Posted 28 January 2013 - 02:14 AM

@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, 28 January 2013 - 02:27 AM.


#5 .josh

.josh

    .josh

  • Administrators
  • 14,745 posts

Posted 28 January 2013 - 09:04 AM

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!

Did I help you? Feeling generous? Donate to me! || Donate to phpfreaks!
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 y

y

    Member

  • Members
  • PipPip
  • 15 posts

Posted 28 January 2013 - 10:27 AM

:-\ sorry .Josh
and yes i've tried nogray's way
window.onunload = goodbye;
no msg appeared at all

what to do ?

#7 .josh

.josh

    .josh

  • Administrators
  • 14,745 posts

Posted 28 January 2013 - 11:48 AM

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;


Did I help you? Feeling generous? Donate to me! || Donate to phpfreaks!
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#8 y

y

    Member

  • Members
  • PipPip
  • 15 posts

Posted 28 January 2013 - 12:49 PM

thanks :)
it works now ... whene using the onbeforeunload event
but only with FF ...... i need it to work with other browsers too

what to do ?

Edited by y, 28 January 2013 - 12:51 PM.


#9 .josh

.josh

    .josh

  • Administrators
  • 14,745 posts

Posted 28 January 2013 - 02:27 PM

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. 

Did I help you? Feeling generous? Donate to me! || Donate to phpfreaks!
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#10 Adam

Adam

    Advanced Member

  • Gurus
  • 5,680 posts
  • LocationSheffield / UK

Posted 28 January 2013 - 07:23 PM

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.
  • y likes this

#11 y

y

    Member

  • Members
  • PipPip
  • 15 posts

Posted 29 January 2013 - 01:30 AM

thank you soooo
i was just trying the event and its respond .... this is not a real script .... i'm still learning
thanks for the good advice ..... the concept is clear now :)

:)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com