Prev: FAQ Topic - How do I check to see if a child window is open, before opening another? (2010-02-18)
Next: Form to JavaScript.
From: Jayesh Modha on 17 Feb 2010 20:07 Hi, I need to give a warning message when the user is trying to refresh (either by F5 / CTRL + F5 or by reload from the context menu) the window. If the user is closing the window, it is fine, no need to give any warning. For this purpose I am using OnBeforeUnload event but OnBeforeUnload also gets fired on closing the window. I can detect if the window is getting closed but it works only for IE not for Firefox or any other browser. I found out that those properties are specific to IE DOM properties not W3C DOM properties. However, I am still interested to figuring this out this for Firefox. The clientY is defined during other events (OnMouseDown, etc) but is undefined within the OnBeforeUnload event which is the reason my code isn't working. Does anyone know any another way to capture this? I even tried OnMouseDown even to see if I can capture the mouse location when the user is clicking on X Close button, but the attempt wasnt successful. I am open to any other ideas/suggestions you may have which works for all browsers. Here's my code ------------------------------------------------------------------- var tryingToReload = false; window.onbeforeunload = function(e) //on before unload { if (!e) //Firefox and Safari gets argument directly. { e = window.event; // this is for IE } if (e.clientY != undefined && e.clientY < 0) // clicked on the close button for IE { tryingToReload = false; } if (e.clientY != undefined && (e.clientY > 100 && e.clientY < 140)) // select close from context menu from the right click on title bar on IE { tryingToReload = false; } if (tryingToReload) //user hasn't clicked on X close button or hasn't selected close from context menu { tryingToReload = false; return "warning message goes here"; } } document.onkeydown = function(e) //attach to key down event to detect the F5 key { tryingToReload = false; if (!e) //Firefox and Safari gets argument directly. { e = window.event; } var key = e.keyCode ? e.keyCode : e.which; try //try { if (key == 116) //F5 Key detected { tryingToReload = true; } } catch (ex) { } } document.oncontextmenu = function(e) //check for the right click { //cannot blindly say tryingToReload = true as the context menu doesn't have refresh/reload options on all the elments. //reload options doesn't appear on image, anchor tag, it does appear on body, td and div tag in this case var srcElement = getEventSrc(e); var tagName = ''; if (srcElement.tagName != undefined) //Get the name of the tag { tagName = srcElement.tagName; } switch (tagName) { case "BODY": case "TD": case "DIV": { tryingToReload = true; break; } default: break; } } function getEventSrc(e) { if (this.Event) { var targ = e.target; //nodeType of 1 means ELEMENT_NODE return targ.nodeType == 1 ? targ : targ.parentNode; } else //this is for IE return event.srcElement; } document.onclick = function(e) //clicked anywhere else, you are not trying to reload { tryingToReload = false; } ------------------------------------------------------------------- Really appreciated you looking into my post. Thanks for your time. Thanks, Jayesh Modha
From: Scott Sauyet on 18 Feb 2010 09:38 On Feb 17, 8:07 pm, Jayesh Modha <jayesh.mo...(a)gmail.com> wrote: > I need to give a warning message when the user is trying to refresh > (either by F5 / CTRL + F5 or by reload from the context menu) the > window. If the user is closing the window, it is fine, no need to give > any warning. I was hoping someone with more specific knowledge would respond. My guess -- and it is only a guess -- is that there will be no way in general to determine if the page is unloaded for a refresh or for a browser window close. It just doesn't seem like a thing that the browser makers would feel a need to expose, and might in fact reveal more of the user's behavior than wanted. I'd be interested to be proven wrong, but that's my guess. Good luck, -- Scott
From: Jayesh Modha on 18 Feb 2010 12:00 Scott, Thanks a lot for your response. Really appreciate you looking into this. I am also thinking the same that browser would not expose a direct way to tell difference between window refresh and window close. However, if you look at my code above, we can specifically tell difference between F5/CTRL+F5 refresh and window close. It also works across all the browsers. There is only one condition which is not working for me with my above code. If you right click on the window, then after you don't select refresh/ reload option and instead you close the window or you right click on the title bar and close window from the context menu. My above code doesn't work in that condition as I am not able to detect the location of the click anyhow. It works just fine with IE as it is giving you location of click and all other browser gives undefined value for clientY property. Hopefully I would get a solution. On Feb 18, 6:38 am, Scott Sauyet <scott.sau...(a)gmail.com> wrote: > On Feb 17, 8:07 pm, Jayesh Modha <jayesh.mo...(a)gmail.com> wrote: > > > I need to give a warning message when the user is trying to refresh > > (either by F5 / CTRL + F5 or by reload from the context menu) the > > window. If the user is closing the window, it is fine, no need to give > > any warning. > > I was hoping someone with more specific knowledge would respond. > > My guess -- and it is only a guess -- is that there will be no way in > general to determine if the page is unloaded for a refresh or for a > browser window close. It just doesn't seem like a thing that the > browser makers would feel a need to expose, and might in fact reveal > more of the user's behavior than wanted. > > I'd be interested to be proven wrong, but that's my guess. > > Good luck, > > -- Scott
From: Scott Sauyet on 18 Feb 2010 12:31 On Feb 18, 12:00 pm, Jayesh Modha <jayesh.mo...(a)gmail.com> wrote: > I am also thinking the same that browser would not expose a direct way > to tell difference between window refresh and window close. > However, if you look at my code above, we can specifically tell > difference between F5/CTRL+F5 refresh and window close. It also works > across all the browsers. Capturing F5/CTRL+F5 seems reasonable. But there are a number of other ways to refresh/reload the page. I tried your code in IE8 and FF3.6 and catch only the F5/CTRL+F5 events. It doesn't capture the refresh with the browsers' refresh/reload buttons. It doesn't capture the context menu reload. It doesn't capture FF's View > Reload. It doesn't capture CTRL-R in FF. Do you have a publicly available page where it's working for you? -- Scott
From: Jayesh Modha on 18 Feb 2010 12:42
Unfortunately, I don't have a public page. I totally forgot CTRL + R, wow, thanks a lot for that. I will include that into my code. Hopefully, I haven't forgot any of my code here but I will zip up my code and send to you if you'd prefer that. On Feb 18, 9:31 am, Scott Sauyet <scott.sau...(a)gmail.com> wrote: > On Feb 18, 12:00 pm, Jayesh Modha <jayesh.mo...(a)gmail.com> wrote: > > > I am also thinking the same that browser would not expose a direct way > > to tell difference between window refresh and window close. > > However, if you look at my code above, we can specifically tell > > difference between F5/CTRL+F5 refresh and window close. It also works > > across all the browsers. > > Capturing F5/CTRL+F5 seems reasonable. But there are a number of > other ways to refresh/reload the page. I tried your code in IE8 and > FF3.6 and catch only the F5/CTRL+F5 events. It doesn't capture the > refresh with the browsers' refresh/reload buttons. It doesn't capture > the context menu reload. It doesn't capture FF's View > Reload. It > doesn't capture CTRL-R in FF. > > Do you have a publicly available page where it's working for you? > > -- Scott |