admin管理员组

文章数量:1201166

I am new to JavaScript and having a slight issue figuring out when events fire. Here is the situation. I am writing an ASP.Net application. In my code behind, in the Page_Load() I have some code. I need this code to execute first. After this code has finished executing, then I need my JavaScript to execute.

Is there an event or something that exists that insures the code will execute in that order?

I am new to JavaScript and having a slight issue figuring out when events fire. Here is the situation. I am writing an ASP.Net application. In my code behind, in the Page_Load() I have some code. I need this code to execute first. After this code has finished executing, then I need my JavaScript to execute.

Is there an event or something that exists that insures the code will execute in that order?

Share Improve this question asked Dec 7, 2011 at 16:12 user489041user489041 28.3k56 gold badges141 silver badges206 bronze badges 0
Add a comment  | 

5 Answers 5

Reset to default 11

The simplest way would be to add your function call to your body's onload event

<body onload="yourFunction()">

function yourFunction() {
}

Alternatively, you could also place some script at the very end of your body. Any script in here will be executed after the body is processed. You'll be able to do things like

var divElement = document.getElementById("divId");

without it coming back as null.

EDIT

I didn't see jQuery tagged on your question, but, if you happen to be using it, you can also do this:

$(document).ready(function() {
    //dom is ready
});

or more simply:

$(function() {
    //dom is ready
});

Scripts included on the body execute when the body has loaded and they execute in order. This can be used as a cross-browser solution for ensuring scripts get loaded after the page has loaded.

<!DOCTYPE html>
<html>
<head>
    <!-- scripts loaded in unknown order in some browsers -->
    <script type="text/javascript" src="headscript.js"></script>
</head>
<body>

    <!-- scripts included on body execute in order when the DOM is ready -->
    <script type="text/javascript">
        (function () {
            // do what you want here without trashing global scope
            // you should probably include this as an external script
        }());
    </script>
</body>   
</html>

Yes, there are several ways to go:

a.

$(window).ready(function() 
{
   //Your code goes here
});

b.

$(document).ready(function() 
{
   //Your code goes here
});

c.

window.onload = function() 
 {
  //Your code goes here
 }

I think you may have a slight confusion around the lifecycle of an ASP page.

All the events in the server-side (VB / C#) code will fire (except maybe for user triggered events) every time the page is built. So Init, Load, PreRender, Render, Unload etc... Only after the page has been built is it sent down the wire to the browser. Once the browser has hold of it then your JavaScript may be executed.

The way Adam suggests is one possible way of doing this.

Fret not, server-side scripts will always execute before client sided ones.

To ensure that the Javascript executes after everything on the page (DOMContent-wise) has been created, you have several options. You can add the script to the very bottom of the tag. This is actually good practice, as a script at the bottom of the page makes the page seem to load faster, and perceived performance is important.

As Adam said, you can also use event handlers, such as window.onload = function(){} or window.addEventListener("load", function(){}, true). An alternative is to use "DOMContentLoaded" instead of "load", which will execute once the entire text portion of the html file loads (as opposed to waiting for all the images, etc. to load). If you put the script at the bottom of the page however, this will be unnecessary.

Here's an example html file. Notice the stylesheet is defined at the top, while the script is defined at the bottom.

<!DOCTYPE html>
<html>
<head>
<title></title>
<link type="text/css" href="style.css" rel="stylesheet"/>
</head>
<body>
<script type="text/Javascript" src="script.js"></script>
</body>
</html>

本文标签: aspnetExecuting Javascript after page has loadedStack Overflow