473,564 Members | 2,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_func tion(); 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 2168
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 addEventListene r and attachEvent

addEventListene r('unload', rainbow, true);

You can detect this with:-

if(window.attac hEvent) {
attachEvent("on unload", rainbow);
} else if(window.addEv entListener) {
attachEvent("un load", 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_func tion(); 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/ addEventListene r 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/addEventListene r 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 addEventListene r and attachEvent

addEventListene r('unload', rainbow, true);

You can detect this with:-

if(window.attac hEvent) {
attachEvent("on unload", rainbow);
} else if(window.addEv entListener) {
attachEvent("un load", 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_func tion(); 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/ addEventListene r 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/addEventListene r pair would work.
I will check out attachEvent, addEventListene r 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/addEventListene r 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 addEventListene r 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
addEventListene r 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.attachE vent){
alert('browser with window.attachEv ent')
}
else if (window.addEven tListener){
alert('browser with window.addEvent Listener')
}
}
/* run on IE 6, 7 etc. */
if(window.attac hEvent){
attachEvent("on unload", rainbow)
}
/* or this for standard browsers */
else if(window.addEv entListener){
addEventListene r('unload', rainbow, true)
}
</script>

However, I don't understand whether to use true or false in the third
argument of the addEventListene r 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
addEventListene r? 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
11208
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 only one column: txtOutput I want to use the DB front end (MS Access) to send the text string to the SQL backend, then have the SQL Server create a...
2
6200
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 onmousedown event could bubble up to an onload event. NOTE: If you put an alert() statement in the onmousedown event handler the body onload event doesn't...
4
7602
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 fill the iframe with content (<body> tag and so on and also insert a <div> tag, inbetween is the content that should be modified). Now if the...
1
1992
by: Robert Karlsson | last post by:
Hi, Situation Frameset with two frames. ------------------ top navigation ------------------ main content ------------------
2
11355
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 address everything before the first period is backwards; and the last period, and everything after it, should be discarded.
2
1304
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 value of x = 10. Setting x = 10 in the onmousemove may be a performance issue. Thank you very much for your advice. After certain seconds....
3
3861
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 least VB 6.0) by calling raise event .... For example,
6
9549
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 = 'http://www.blah blah blah.com'; But when I try to set an event handler this way, as shown below, it fails. parent.ToolMenuFrame.window.document.body.onload =...
0
7666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.