Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hook a javascript event to page load

I have an aspx that has the following javascript function being ran during the onload event of the body.

<body onload="startClock();"> 

However, I'm setting the aspx up to use a master page, so the body tag doesn't exist in the aspx anymore. How do I go about registering the startClock function to run when the page is hit and still have it use a masterpage?

like image 254
Jagd Avatar asked Jun 03 '09 18:06

Jagd


People also ask

How do I trigger a function on page load?

The first approach for calling a function on the page load is the use an onload event inside the HTML <body> tag. As you know, the HTML body contains the entire content of the web page, and when all HTML body loads on the web browser, it will call the function from the JavaScript.

How do you load a page in JavaScript?

Approach: We can use window. location property inside the script tag to forcefully load another page in Javascript. It is a reference to a Location object that is it represents the current location of the document. We can change the URL of a window by accessing it.

How do I run JavaScript as soon as page loads?

If you are using an <script> inside <head> then use the onload attribute inside the <body> tag to Execute JavaScript after page load. It will run the function as soon as the webpage has been loaded.

Does JavaScript run on page load?

onload runs after page load and all javascript is available, so the codeAddress() function can be declared anywhere within the page or linked js files. It doesn't have to come before unless it were called during the page load itself. @Jared Yes it does.


1 Answers

If you don't want to explicitly assign window.onload or use a framework, consider:

<script type="text/javascript"> function startClock(){     //do onload work } if(window.addEventListener) {     window.addEventListener('load',startClock,false); //W3C } else {     window.attachEvent('onload',startClock); //IE } </script> 

http://www.quirksmode.org/js/events_advanced.html

like image 199
Corbin March Avatar answered Oct 08 '22 12:10

Corbin March