RobG said:
Try:
window.onunload = function () { alert('s'); };
or
function s (){
alert('s');
}
window.onunload = s;
Both of which will annoy users of IE also.
Well, strangely enough, it is not IE which is causing me the problems, as it
works in IE in both ways stated above, however, in Firefox, the only way I
can get the function to actually happen when the onunload event occurs on
the browser is to use the following.
function s (){
alert('s');
}
window.onunload = s();
The different being that the function name MUST have parentheses after it
for that function to get run onunload. With this format, IE doesn't work
correctly, which it shouldn't. So, without browser-sniffing, I can't seem
to figure a way to get it to work.
window.onunload is DOM level 0 and not part of the W3 spec. Mozilla seems
to allow the body onunload to be added dynamically but doesn't execute it
when the page unloads. Where onunload exists in the source HTML, it seems
to work as expected.
Adding window.onunload dynamically seems to work in Mozilla and IE.
<URL:
http://www.mozilla.org/docs/dom/domref/dom_window_ref75.html>
IE seems to be honoring this syntax but I can't get Firefox to work any
other way than to have the parentheses following the function name, such as
window.onunload = funcRef()
The use of onunload is not encouraged for general web pages, its principal
use initially having been to create 'pop-up hell'. I hope your intentions
are more honourable.
To explain to you a bit more of what I'm trying to do, I have created a
shopping-cart style application, and when the user wants to view the
shopping cart, it opens a new window with the shopping cart information in
it. If the user changes web sites or refreshes the page, I do not want the
shopping cart window to stay open, so I want to put an onunload event on the
BODY element which will automatically close the shopping cart window if it
is open. The reason I must set this dynamically is that this may get
integrated into an HTML template, which means I don't have any control over
the HTML template and I can't add/change the template (except at runtime),
so any stylesheet link tags or script file dependencies must be added at
runtime. In addition, I must add my onunload event to the BODY tag
dynamically since I don't have control over the HTML source. The only way I
could possibly put a static onunload event in the HTML would be to have my
PHP code automatically edit the templates to include the onunload event in
there, but I would prefer to avoid that option for obvious reasons.
Now, another problem I discovered with Firefox is that when I do use the
obj.onunload=funcdef() syntax, then I can get an alert to display onunload.
However, when I replace the alert with my desired code, I am getting an
error (which I do not get in IE). What I am doing is checking to see if my
windowhandle variable exists and if it does, I am using the statement
winDVDCart.close(), where winDVDCart is my window handle. In Firefox I am
getting the error "winDVDCart has no properties" as if my handle to that
window object has already been cleared before this event code ran. But if I
actually DO put the onunload="..." in the HTML on the BODY tag, it will
close the window without a problem. Same code, same function, just defined
statically instead of dynamically.
It appears that Firefox has it's own share of things that aren't exactly
working as defined so if anyone could give me an idea of what to do here I
would greatly appreciate it. I haven't tried the window.addEventListener()
method yet but, once again, if I did that I would have to use
browser-sniffing because IE doesn't yet support the addEventListener()
method. If someone would like more information or explanation on what I'm
trying to do I'd be more than happy to explain and demonstrate with some
real code clips. Thanks for the help so far.
David