• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8607
  • Last Modified:

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...
0
Maverick_Cool
Asked:
Maverick_Cool
  • 19
  • 11
  • 3
  • +2
1 Solution
 
Göran AnderssonCommented:
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?
0
 
Maverick_CoolAuthor Commented:
yes the iframe is removed totally, i want the window.document.unload to be  triggered.
0
 
Michel PlungjanIT ExpertCommented:
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
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Göran AnderssonCommented:
> 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.
0
 
Michel PlungjanIT ExpertCommented:
Ok. I was sure a removal of the iframe would not trigger anything...

Interesting
0
 
Maverick_CoolAuthor Commented:
whats the work around?
0
 
Maverick_CoolAuthor Commented:
i tried mplungian method it said object doesnt support this property.
0
 
Maverick_CoolAuthor Commented:
GreenGhost: is not working...
0
 
Göran AnderssonCommented:
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

0
 
Maverick_CoolAuthor Commented:
you are not removing the element<iframe>
0
 
Maverick_CoolAuthor Commented:
then my unload function wont find the body only. more it will working 2 threads,
0
 
Maverick_CoolAuthor Commented:
can i directly calll the onunload or onbeforeunload function using the iframe's content Window or document obj
0
 
Göran AnderssonCommented:
> 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.
0
 
Maverick_CoolAuthor Commented:
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.
0
 
Maverick_CoolAuthor Commented:
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?
0
 
Maverick_CoolAuthor Commented:
is there way through i can raise event or call event handler directly...
0
 
Göran AnderssonCommented:
> 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?
0
 
Maverick_CoolAuthor Commented:
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.
0
 
Maverick_CoolAuthor Commented:
i have increased the points to 500 ,plz help me with this question.
0
 
Göran AnderssonCommented:
> 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...
0
 
Maverick_CoolAuthor Commented:
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.
0
 
Michel PlungjanIT ExpertCommented:
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>
0
 
Göran AnderssonCommented:
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.
0
 
Maverick_CoolAuthor Commented:
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.
0
 
Göran AnderssonCommented:
No, an iframe does not run separately from the page.

This can easily be demonstrated by code like the following: There is an interval running in the iframe, updating a counter ten times a second. There is a button in the page that runs a delay loop. When you press the button, the interval in the iframe is suspended until the loop finishes.

This is the reason why the onunload event doesn't fire in the iframe as long as you are running a script in the page. The only solution to this is to write your own browser that works differently.
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">
<head>
<title>test</title>
</head>
<body>
<div id="container">
<iframe src="test2.html" id="frame"></iframe>
</div>
<input type="button" value="Pause" onclick="for(var i=0;i<10000000;i++);" />
</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 onload="window.setInterval('document.body.innerHTML=++cnt;',100);">
</body>
</html>

Open in new window

0
 
Maverick_CoolAuthor Commented:
but the iframes Content document does.
0
 
Maverick_CoolAuthor Commented:
and comment was on refreshing the page, the idea you gave me.
0
 
Göran AnderssonCommented:
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.
0
 
Maverick_CoolAuthor Commented:
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

0
 
Maverick_CoolAuthor Commented:
typo error samll change
document.getElementById('frame').contentWindow.location.href=document.getElementById('frame').contentWindow.location.href;
0
 
Göran AnderssonCommented:
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.
0
 
Maverick_CoolAuthor Commented:
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?"
0
 
Göran AnderssonCommented:
> 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.
0
 
Vee_ModCommented:
Force accepted.
Vee_Mod
Community Support Moderator
0
 
aliptaCommented:
so no Experts to solve
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 19
  • 11
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now