Krishna Thota
Krishna Thota

Reputation: 7036

Making Javascript Code run on Page Start

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)

Upvotes: 2

Views: 217

Answers (2)

Rafael Sedrakyan
Rafael Sedrakyan

Reputation: 2629

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(){

});

Upvotes: 1

Arturo
Arturo

Reputation: 1089

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

Upvotes: 1

Related Questions