y Posted January 27, 2013 Share Posted January 27, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/273695-error-with-onunload-event/ Share on other sites More sharing options...
.josh Posted January 27, 2013 Share Posted January 27, 2013 so are you saying you get that "Thank you for visiting. come back soon!" message when your page first loads? Quote Link to comment https://forums.phpfreaks.com/topic/273695-error-with-onunload-event/#findComment-1408571 Share on other sites More sharing options...
nogray Posted January 27, 2013 Share Posted January 27, 2013 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; Quote Link to comment https://forums.phpfreaks.com/topic/273695-error-with-onunload-event/#findComment-1408585 Share on other sites More sharing options...
y Posted January 28, 2013 Author Share Posted January 28, 2013 (edited) @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 January 28, 2013 by y Quote Link to comment https://forums.phpfreaks.com/topic/273695-error-with-onunload-event/#findComment-1408685 Share on other sites More sharing options...
.josh Posted January 28, 2013 Share Posted January 28, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/273695-error-with-onunload-event/#findComment-1408756 Share on other sites More sharing options...
y Posted January 28, 2013 Author Share Posted January 28, 2013 :-\ sorry .Josh and yes i've tried nogray's way window.onunload = goodbye; no msg appeared at all what to do ? Quote Link to comment https://forums.phpfreaks.com/topic/273695-error-with-onunload-event/#findComment-1408783 Share on other sites More sharing options...
.josh Posted January 28, 2013 Share Posted January 28, 2013 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; Quote Link to comment https://forums.phpfreaks.com/topic/273695-error-with-onunload-event/#findComment-1408793 Share on other sites More sharing options...
y Posted January 28, 2013 Author Share Posted January 28, 2013 (edited) 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 January 28, 2013 by y Quote Link to comment https://forums.phpfreaks.com/topic/273695-error-with-onunload-event/#findComment-1408807 Share on other sites More sharing options...
.josh Posted January 28, 2013 Share Posted January 28, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273695-error-with-onunload-event/#findComment-1408821 Share on other sites More sharing options...
Adam Posted January 29, 2013 Share Posted January 29, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273695-error-with-onunload-event/#findComment-1408855 Share on other sites More sharing options...
y Posted January 29, 2013 Author Share Posted January 29, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/273695-error-with-onunload-event/#findComment-1408888 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.