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?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With