2

I need to run my JavaScript Code on Page Start.

<body onload="document.getElementById('Label1').style.display = 'none';document.getElementById('Textbox1').style.display = 'none';">

Currently I'musing a Onload property in Body Tag and using the code in that. By using in this way the code is running after the total page is loaded and it takes some time to make the label and the textbox to disappear.

Is there any way to run this code before the page Loads??

Or some other way in which I can accomplish this ?

(I'm using another JavaScript function to make the Label and Textbox Visible and Invisible basing on a DropdownList SelectedIndex so if I use properties like style="display:none" I could not make them visible again using JavaScript)

1
  • 1
    You would be better off using the JQuery $(function () {}); rather than using the on load method of the HTML body. Commented Aug 18, 2012 at 8:15

2 Answers 2

1

You don't need to make your html elements invisible with javascript when the page is being loaded. You can set them invisible with css using visibility:hidden or display:none and then set them visible with javascript like this:

document.getElementById("someobject").style.display="block";
OR
document.getElementById("someobject").style.visibility="visible";

Also if you are using jquery you can use

$(document).ready(function(){

});
Sign up to request clarification or add additional context in comments.

4 Comments

How can we make them visible using style="display:none" and set "display:block" with javascript??
Thank you @Rafael Sedrakyan. I've set the style="display:none" to the textbox & label and set document.getElementById("lbl_OtherProof").style.display = "block"; in JavaScript. I don't want to use JQuery for very minor things so I didn't try JQuery.
You're welcome. That is why I wrote "If you are using jquery" :)
Yep Gotcha. Thanks again. Bye
1

Use jquery's $(document).ready(function(){ this is my code; });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.