Link to home
Start Free TrialLog in
Avatar of Maverick_Cool
Maverick_CoolFlag for India

asked on

document unload event doest get invoked on iframe close.

I have iframe which has htm page with an event document unload, but when iframe is closed this event is not triggered.
Is there work around to make this work...
Avatar of Göran Andersson
Göran Andersson
Flag of Sweden image

You can't close an iframe. What do you mean really when you say that the "iframe is closed"? Do you remove the iframe from the page? Do you close the window containing the page having the iframe?
Avatar of Maverick_Cool

ASKER

yes the iframe is removed totally, i want the window.document.unload to be  triggered.
Forget it.

You must trigger it yourself:

window.frames[0].onunload()
....

assuming the iframe's content is from the same site as the page hosting the iframe
> yes the iframe is removed totally, i want the window.document.unload to be  triggered.

It is.

If you have an onunload event in the document loaded in an iframe, it will be triggered:

1. If the document in the iframe is unloaded (src changes).

2. If the iframe is removed from the document.

3. If the document containing the iframe is unloaded (location changes).

4. If the tab or window is closed.

I tested all these, in Firefox and Internet Explorer.

The only occasion that I can find where the onunload event is not triggered, is if you hide the iframe using stlye.display='none', but then the document is still loaded in the iframe even if it's not visible, so it should not be triggered.

> You must trigger it yourself

Nope. It's triggered automatically.
Ok. I was sure a removal of the iframe would not trigger anything...

Interesting
whats the work around?
i tried mplungian method it said object doesnt support this property.
GreenGhost: is not working...
Yes, it's working.

Here is an example. Pressing the button removes the iframe, which triggers the onunload event in the document in the iframe.

Also, refreshing the page triggers the event. Refreshing the document in the iframe triggers the event. Closing the window triggers the event.
test.html:
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<html>
<head>
<title>test</title>
 </head>
<body>
<div id="container">
<iframe src="test2.html" id="frame"></iframe>
</div>
<input type="button" value="Remove" onclick="document.getElementById('container').innerHTML='';" />
</body>
</html>
 
test2.html:
 
<!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" xml:lang="en" lang="en">
<head>
<title>test</title>
</head>
<body onunload="alert(1);">
iframe
</body>
</html>

Open in new window

you are not removing the element<iframe>
then my unload function wont find the body only. more it will working 2 threads,
can i directly calll the onunload or onbeforeunload function using the iframe's content Window or document obj
> you are not removing the element<iframe>

What do you mean?

> then my unload function wont find the body only.

What unload function? Do you mean that you just named a function "unload" and expect it to catch the event automatically, or what?

What does your code look like?

> more it will working 2 threads,

What do you mean? There is no multi threading in Javascript. It's strictly single threaded.
see, the content window/document has it own script and events , now when in the parent window/document i do innerHTML = '', thogh the content document of frame is called , but it has its own pace ,in sense a seperate thread/lazy loading, sometime may be till unload is completes, the parent document has already removed the div contenting the iframe, hence my code in unload event of iframe will not get doc/body.
one work around which partailly worked was to put event handler onbeforeunload and do a contentWindow.document.location.href = ..contentWindow.document.location.href;
here though it stops, incase int handler and i want stop unloading, i can't, more onbeforeunload is only I.E.
Browser compatibility?
is there way through i can raise event or call event handler directly...
> it has its own pace ,in sense a seperate thread/lazy loading

Actually the contrary. If you have any problems, it's probalby because it doesn't have it's own thread.

The term "lazy loading" is something completely different, and has nothing to do with this subject.

> sometime may be till unload is completes

Are you talking about some kind of delay?

> the parent document has already removed the div contenting the iframe, hence my code in unload event of iframe will not get doc/body.

Which "doc/body" are you talking about, and why would you need to "get" it?

> incase int handler and i want stop unloading, i can't

What?

I demonstrated that the onunload event does work. What is it in your code that makes it different from the one I presented? What does your code look like?
it need the content window document, because my unload function will check dirty fields, id so will notify the user to save it and stop the unload option.

as far previous comment, i think ur not getting point .

I will explain whats happening , as soon as execute the stmt say:
document.getElementById('container').innerHTML='';

it doesnot wait until the unload event handler completes it action.
but it goes to next stmt and executes it.
i have increased the points to 500 ,plz help me with this question.
> so will notify the user to save it and stop the unload option.

If you remove the iframe from the document, it doesn't help to stop the unloading of the document in the imframe. Eventhough the document in the iframe isn't unloaded, it still doesn't stop the iframe from being removed.

> it doesnot wait until the unload event handler completes it action.

No, actually the unload event doesn't start at that time. As Javascript is single threaded, no events occur while some code is running. You have to return control to the browser by exiting the code for any events to occur.

I still haven't seen any of your code, and I still don't have a clear picture of what you are trying to do, so it's still a lot of guesswork...
ok, just tellin method to do this 2 operation:

1.> How to access onunload function of contentWindow's document from the div containing the iframe.

2.>if a iframe reloading or unload how to stop it depending on some condition.
help me this i have a work around.
I cannot call the onunload from a link


<a href="#" onClick="window.frames[0].onunload(); return false">onUnload does not do it</a>

<a href="#" onClick="document.getElementById('frameContainer').innerHTML=''; return false">remove the frame - WILL trigger</a>
<div id="frameContainer">
<iframe src="unloadtest2.html"></iframe>
</div>

where the iframe is

<script>
window.onunload = function() {
alert('unloading')
}
</script>

<body >
test unload
<script>
window.onunload(); // THIS triggers just fine
</script>
</body>
I don't feel that the thread should be deleted. It contains quite some information about how the onunload event works in an iframe, that might be useful to someone. I don't think that this information should be deleted just because the Asker won't show any of the code he is using, so that he could get any help with it.
answer given  are work arounds which is not helping my need. And as i dont see a resolution and i wanted to delete or close.

Any way,
Referring to Green Ghost comment:
"As Javascript is single threaded, no events occur while some code is running. You have to return control to the browser by exiting the code for any events to occur."

this is not in case you iframe , each iframe is new window hosting its script, hence it may run can be separately simultaneously.
ASKER CERTIFIED SOLUTION
Avatar of Göran Andersson
Göran Andersson
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
but the iframes Content document does.
and comment was on refreshing the page, the idea you gave me.
No it doesn't.

Javascript in the browser is strictly single threaded. This is not limited to iframes in a page, all windows in the same instance of the browser share the same Javascript engine. While script runs in one window, all Javascript events in all other windows in the instance are suspended.
try this example:
alert of 1 will come before , alert of 2,
This what i meant.

below code is slight modification of your code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>test</title>
</head>
<body>
<div id="container">
<iframe src="test2.html" id="frame"></iframe>
</div>
<input type="button" value="reload" onclick="document.getElementById('frame').contentWindow.location.href=document.getElementById('frame').contentWindow/location.href;alert(1);" />
</body>
</html>
 
//test2.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>test</title>
<script>
var cnt = 0;
</script>
</head>
<body onunload="alert(2);">
</body>
</html>

Open in new window

typo error samll change
document.getElementById('frame').contentWindow.location.href=document.getElementById('frame').contentWindow.location.href;
Yes, that is exactly what I am talking about. The onunload event doesn't occur until you complete the script in the page, so that the browser can start handling events again.
please refer my comment :
http:#20859038 and http:#20860234
and yours :
http:#20859752
that will explain all answers.
you were fully concreted onunloads works , but the gist was "When?"
> you were fully concreted onunloads works

That is because it does, as I demonstrated.

> but the gist was "When?"

That's why have repeatedly asked what your code looks like. As I demonstrated that the event itself works just fine, your code must be doing something differently to keep it from working.
Force accepted.
Vee_Mod
Community Support Moderator
Avatar of alipta
alipta

so no Experts to solve