Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
52
rated 0 times [  59] [ 7]  / answers: 1 / hits: 22922  / 13 Years ago, wed, december 7, 2011, 12:00:00

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?


More From » asp.net

 Answers
8

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
});

[#88693] Tuesday, December 6, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rocky

Total Points: 316
Total Questions: 108
Total Answers: 110

Location: Mali
Member since Sat, Feb 12, 2022
2 Years ago
;