Open Bug 197709 Opened 21 years ago Updated 2 years ago

window.onunload handler is fired on load

Categories

(Core :: DOM: Events, defect, P5)

defect

Tracking

()

People

(Reporter: roman, Unassigned)

References

(Depends on 1 open bug, )

Details

Attachments

(1 file)

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.3) Gecko/20030312
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.3) Gecko/20030312

function wontseeme () { alert("you'll never see this"); };
function willseeme () { alert("I pop up when I should not"); };
otherwin = window.open('other.html', ...);
otherwin.onload = wontseeme;
otherwin.onunload = willseeme;


Reproducible: Always

Steps to Reproduce:

Actual Results:  
otherwin.onunload fires when the window is loading.
otherwin.onload does not fire at all.
the js console remains quiet.

Expected Results:  
otherwin.onload is fired when the window loads up.
otherwin.onunload is fired when the page is being left, or when the window is
being closed.

I'll attach a test case once I have the PR number.
btw, if you put

    <head>
        <script language="javascript1.3">
            window.onload   = opener.inspect;
            window.onunload = opener.inspect;
        </script>
    </head>

into the popup window instead of assigning the event handlers in the opener,
both events are fired when they should (as witnessed by adding alert(o.type)
into the inspect() function).
Sounds like the handlers are attached before the actual load starts, so you're
attaching them to the about:blank document in the new window....
so, is this a bug, or is the behavior undefined? what is the proper way of
decorating a window with code it doesn't know about?
addEventListener is the correct W3 way to get status on a window without
perturbing it, and this is a bug. It has been partially fixed in 1.6b. The load
event now fires: see bug 226416 comment 11 on down. But at this time the unload
event still fires as well.

The problem is a temporary document that's placed in the new window as a
placeholder, to not confuse ubiquitous code that assumes a window will have a
document. That seems like a mistake, but there we are.
Assignee: saari → general
Status: UNCONFIRMED → NEW
Ever confirmed: true
OS: Windows 2000 → All
Hardware: PC → All
Does the temporary about:blank document really need to fire the events?
fixing the summary for better search results ( windlow. -> window. )
Summary: windlow.onunload handler is fired on load → window.onunload handler is fired on load
I'm not completely certain. Sometimes the temporary document is never replaced,
so it's not temporary at all. It's probably a 90% solution to mute the temporary
document. It's probably an 80% solution to simply make all about:blank documents
mute.

The only case I can think of where the 90% solution fails is when opening a
window with no URL at all; that is, window.open(), no parameters. Since that's
rarely done and I'm willing to claim it's even more rare that anyone cares to
attach event listeners to such a window, it may be more like a 99% solution. But
I could be overlooking something.
Depends on: 227028, 227605
Comment on attachment 194949 [details]
Type in an address or click the link, you'll notice if you view the source, it is the page you should be at.  But all you see is the unload.html page.

<HTML><HEAD>
<SCRIPT>
window.unlock = false;
window.onunload=function()
{
if(!window.unlock)
 alert("You can't leave");
 window.location.href='attachment.cgi?id=194949';
}
document.onmousedown=function()
{
  window.unlock = true;
}
</SCRIPT></HEAD>
<BODY>
<A href="http://www.google.com">Link</A><BR/>
</BODY></HTML>
Comment on attachment 194949 [details]
Type in an address or click the link, you'll notice if you view the source, it is the page you should be at.  But all you see is the unload.html page.

<HTML><HEAD>
<SCRIPT>
window.unlock = false;
window.onunload=function()
{
if(!window.unlock)
 alert("You can't leave");
 window.location.href='attachment.cgi?id=194949';
}
document.onmousedown=function()
{
  window.unlock = true;
}
</SCRIPT></HEAD>
<BODY>
<A href="http://www.google.com">Link</A><BR/>
</BODY></HTML>
When I open() a specific uri, the unload event should not trigger when about:blank gets unloaded to make room for the requested uri.
Assignee: general → nobody
QA Contact: desale → events
Does this Bug still apply? Is it related to Bug 663753?
https://bugzilla.mozilla.org/show_bug.cgi?id=1472046

Move all DOM bugs that haven't been updated in more than 3 years and has no one currently assigned to P5.

If you have questions, please contact :mdaly.
Priority: -- → P5
Severity: normal → S3
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: