473,392 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,392 software developers and data experts.

Q: Create unUnload event trigger outside body tag?

Is it possible to add a function call to the onUnload event handler from an
external js file, or can this only be done via the body tag?

In any case, I presume there can only exist one onUnload event handler,
which can naturally include any number of function calls.

I have instances of html pages, with an existing onUnload event handlers
and those without.

I'd like to cover both instances, if possible, so that if there is an
onUnload call already defined in the body tag, to attach or add a function
to that existing event handler, while if there is none, create the onUnload
event handler via a js file, along with some function it should launch.

In other words, if 'page1.html' already contains ..
<body onUnload="some_function()">
... while if 'page2.html' contains only <bodywithout an onUnload call, and
if both pages shares a one and same external 'include.js' file with
some_other_function(); to be called onUnload, can: 1) the result for
page1.html be made to do the same as if the following was hardcoded in the
body tag:
<body onUnload="some_function();some_other_function();">

But in case of page2.html, the result would be the same as if this alone
was in the body tag:
<body onUnload="some_other_function();">

I guess this would be a kind of dymanic event handler and function
depending on whether or not the onOnload handler exists in the body tag
already before. Is this at all possible?

Thanks,
Tuxedo
Sep 4 '08 #1
3 2158
Tuxedo wrote:
Is it possible to add a function call to the onUnload event handler from an
external js file, or can this only be done via the body tag?
You can use window.onunload:

window.onunload = rainbow;

function rainbow() {
confirm("Do you like Ritcie Blackmore?");
}

In any case, I presume there can only exist one onUnload event handler,
which can naturally include any number of function calls.
Yes, but you could also use addEventListener and attachEvent

addEventListener('unload', rainbow, true);

You can detect this with:-

if(window.attachEvent) {
attachEvent("onunload", rainbow);
} else if(window.addEventListener) {
attachEvent("unload", rainbow, true);
}
I have instances of html pages, with an existing onUnload event handlers
and those without.

I'd like to cover both instances, if possible, so that if there is an
onUnload call already defined in the body tag, to attach or add a function
to that existing event handler, while if there is none, create the onUnload
event handler via a js file, along with some function it should launch.

In other words, if 'page1.html' already contains ..
<body onUnload="some_function()">
.. while if 'page2.html' contains only <bodywithout an onUnload call, and
if both pages shares a one and same external 'include.js' file with
some_other_function(); to be called onUnload, can: 1) the result for
page1.html be made to do the same as if the following was hardcoded in the
body tag:
<body onUnload="some_function();some_other_function();">

You could use an Event Registry to add/remove legacy onunload handler.

It would be simpler to use attachEvent/ addEventListener pair.
But in case of page2.html, the result would be the same as if this alone
was in the body tag:
<body onUnload="some_other_function();">

I guess this would be a kind of dymanic event handler and function
depending on whether or not the onOnload handler exists in the body tag
already before. Is this at all possible?
That's what an Event Registry would handle. An Event Registry leverages
the fact that functions are objects that can be passed around. Like:-

var oldeventhandler = obj[type];
obj[type] = function(e) {
oldeventhandler(obj, e);
newhandler.call(obj, e);
};
- but a Registry would keep the object's event handlers in an array.
There is a wrapper that has the object, an event type, and a callstack.
The wrapper adds a callback to the object for that event type (obj[type]
= fireEvent());.

fireEvent is a function that returns a function. It does this to take
advantage of enclosing scope.

I wrote an article about that very subject here:-
http://dhtmlkitchen.com/?category=/u...ication-System

But for your case, you might not need all that, in which case a simple
attachEvent/addEventListener pair would work.

Garrett
Thanks,
Tuxedo
Sep 4 '08 #2
dhtml wrote:
Tuxedo wrote:
Is it possible to add a function call to the onUnload event handler from
an external js file, or can this only be done via the body tag?

You can use window.onunload:

window.onunload = rainbow;

function rainbow() {
confirm("Do you like Ritcie Blackmore?");
}

In any case, I presume there can only exist one onUnload event handler,
which can naturally include any number of function calls.

Yes, but you could also use addEventListener and attachEvent

addEventListener('unload', rainbow, true);

You can detect this with:-

if(window.attachEvent) {
attachEvent("onunload", rainbow);
} else if(window.addEventListener) {
attachEvent("unload", rainbow, true);
}
I have instances of html pages, with an existing onUnload event handlers
and those without.

I'd like to cover both instances, if possible, so that if there is an
onUnload call already defined in the body tag, to attach or add a
function to that existing event handler, while if there is none, create
the onUnload event handler via a js file, along with some function it
should launch.

In other words, if 'page1.html' already contains ..
<body onUnload="some_function()">
.. while if 'page2.html' contains only <bodywithout an onUnload call,
and if both pages shares a one and same external 'include.js' file with
some_other_function(); to be called onUnload, can: 1) the result for
page1.html be made to do the same as if the following was hardcoded in
the body tag:
<body onUnload="some_function();some_other_function();">


You could use an Event Registry to add/remove legacy onunload handler.

It would be simpler to use attachEvent/ addEventListener pair.
But in case of page2.html, the result would be the same as if this alone
was in the body tag:
<body onUnload="some_other_function();">

I guess this would be a kind of dymanic event handler and function
depending on whether or not the onOnload handler exists in the body tag
already before. Is this at all possible?

That's what an Event Registry would handle. An Event Registry leverages
the fact that functions are objects that can be passed around. Like:-

var oldeventhandler = obj[type];
obj[type] = function(e) {
oldeventhandler(obj, e);
newhandler.call(obj, e);
};
- but a Registry would keep the object's event handlers in an array.
There is a wrapper that has the object, an event type, and a callstack.
The wrapper adds a callback to the object for that event type (obj[type]
= fireEvent());.

fireEvent is a function that returns a function. It does this to take
advantage of enclosing scope.

I wrote an article about that very subject here:-
http://dhtmlkitchen.com/?category=/u...ication-System
>
But for your case, you might not need all that, in which case a simple
attachEvent/addEventListener pair would work.
I will check out attachEvent, addEventListener and fireEvent as well as
your article on the event notification system.

Many thanks!
Tuxedo

Sep 5 '08 #3
dhtml wrote:

[...]
But for your case, you might not need all that, in which case a simple
attachEvent/addEventListener pair would work.
True, no need for major plug-in type libraries in this case.

At first I didn't realise attachEvent was for IE and addEventListener for
the better breed of browsers.

Anyway, all works in that if an onUnload call is present in the body tag
already, when the page unload event takes place, the attachEvent or
addEventListener will not overwrite any existing body call function, which
is exactly what I was looking for. For example:

<body onUnload="alert('hello from body tag')">

<script>
function rainbow(){
if (window.attachEvent){
alert('browser with window.attachEvent')
}
else if (window.addEventListener){
alert('browser with window.addEventListener')
}
}
/* run on IE 6, 7 etc. */
if(window.attachEvent){
attachEvent("onunload", rainbow)
}
/* or this for standard browsers */
else if(window.addEventListener){
addEventListener('unload', rainbow, true)
}
</script>

However, I don't understand whether to use true or false in the third
argument of the addEventListener function.

With the mozilla/firefox based browsers I'm testing the above example on
the effect is the same whether true or false, and although I find it
described on the following page I don't understand it:
http://developer.mozilla.org/En/DOM/...dEventListener

Does anyone have a simple example whereby true or false is relevant in
addEventListener? I guess it makes no difference on IE and with attachEvent
since the argument does not appear to exist.

Thanks for any tips!
Tuxedo
Sep 7 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Lauren Quantrell | last post by:
Is there a way to create a text file (such as a Windows Notepad file) by using a trigger on a table? What I want to do is to send a row of information to a table where the table: tblFileData has...
2
by: Bender | last post by:
Hi, I am wanting to capture an onmousedown event without firing the body tags onload event. Also, if anyone could explain why this happens that would be excellent. I can't see how an...
4
by: Thomas | last post by:
Hi there, I have an iframe which is editable (designMode = "on") and want to resize it dynamically as the content grows (e.g. more lines of text is in there) and there the struggle starts. I...
1
by: Robert Karlsson | last post by:
Hi, Situation Frameset with two frames. ------------------ top navigation ------------------ main content ------------------
2
by: kj | last post by:
How does one trigger an event programmatically? I'm interested in how to do this in both the "Level 0" event model as well as in the DOM Level 2 event model. Thanks! kj -- NOTE: In my...
2
by: Nelson | last post by:
Here is the discussion that is already discussed that I am looking for. After visiting the link below I am getting the following code. My question is in which client side script event I can set the...
3
by: George | last post by:
Hi, I have searched on the net quite a bit, but the more I read, the more confused I get. I want to see if I can raise an event out side of the class. I believe this can be done in VB (at...
6
by: pronerd | last post by:
Hi, I am trying to dynamically set an event handler across frames. I have no problems setting properties across frames doing something like parent.ToolMenuFrame.location.href =...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.