Solved

Move onload/onunload from body tag to script

Posted on 2008-06-22
1
1,193 Views
Last Modified: 2012-06-21
<body onload="initialize();" onunload="GUnload()">

I want to take the body onload and onunload events and place them in a script instead of the body tag.
0
Comment
Question by:hankknight
1 Comment
 
LVL 19

Accepted Solution

by:
LordOfPorts earned 500 total points
ID: 21841675
Because of the timing related to the sequence of events this does not seem to be possible, i.e. if you place it in the <head> section document.body will be NULL and if you place it just prior to the </body> tag it also does not appear to function properly however you can set the window.onload and window.onunload attributes through code, the executes successfully in both IE and FF:

<body>

<script type="text/javascript">

window.onload = initialize;
window.onunload = GUnload;

</script>
</body>

In the code snippet below is a complete example web page that tested successfully in IE and FF.

Someone else might however have a better suggestion.
<!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">

<head>

    <title>Events Through Script</title>

    <script type="text/javascript">

    // <![CDATA[

    

    function initialize()

    {

        alert("Hello from initialize()");

    }

    

    function GUnload()

    {

        alert("Hello from GUnload()");

    }

    

    // ]]>

    </script>

</head>

<body>
 

<script type="text/javascript">

// <![CDATA[

window.onload = initialize;

window.onunload = GUnload;

// ]]>

</script>

</body>

</html>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now