Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run script after page is loaded

Status
Not open for further replies.

SaRiD

Programmer
Apr 19, 2003
45
0
0
Hi

Hopefully this is nice and simple.

Basicly I have a script, which is working fine, but I want it to run first thing once the page has finished LOADING.

The script I want to run hides a few fields on the page. The problem is, is that the fields haven't loaded yet so when the script has run an error is appearing saying the object doesn't exist. After the page has loaded if I execute the script (say on an onclick event) it does what it should do.

Also for other reasons (which I don't really want to get into) I don't want to use a stylesheet to automaticly make the fields hidden.

Make sense?
 
>but I want it to run first thing once the page has finished LOADING
That's what the onload handler is for. Make all the script lines into a function (say onloadhandler()). The simpler method is then to call it from the body tag.
[tt]<body onload="onloadhandler()">[/tt]
 
Another way to achieve the same result is to attach an onload to the window itself...
Code:
<script type="text/javascript">
onload = theFunctionToRunAtLoad;
</script>
That way you can put it into an included .js file if you want.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
If it only it was all that simple :S

I don't want to get into it to much but I won't have access to the <body> tag. The whole page is dynamicaly generate and this javascript code won't always be there. It will only appear depending on certain settings being checked.

This is the script I am trying to run:

Code:
<script type='text/javascript'> 
onLoad = "ShowField('Field Name')"; 
</script>

Thats a basic script as you said above. Unfortuantly nothing happens?
If I add the code: alert('appear'); so that I message comes up to tell me when the script is executed I find that the script it in fact running BUT the page hasn't loaded completely yet.

I also tried adding the alert code into the function ShowField to see if was executing and it is not. But if I execute the ShowField function after the page as loaded then the alert box appears.

This is the process:
1. The header of the page appears
2. Then there is white space where the form is about to appear
3. The onLoad event is run and the message box appears
4. After I click ok
5. The rest of the form appears
6. The page footer appears and the page is complete.

I want to have step 3 executing after step 5, or even step 6 because until step 5 is complete step 3 will be a waste of time.

Make sense?
 
>If it only it was all that simple :S
>I don't want to get into it to much but I won't have access to the <body> tag
>This is the script I am trying to run:
>CODE
>[tt]<script type='text/javascript'>
>onLoad = "ShowField('Field Name')";
></script>[/tt]

If you have no access to anything, then it is not...
You show your code excerpt, you mean you have that kind of access and rights to add script to a page, then, maybe this.
[tt]
<script type='text/javascript'>
window.onLoad = function() {ShowField('Field Name')};
function ShowField(sFieldName) {
//etc etc... the actual function instructions
}
</script>
[/tt]

 
Thanks for your help tsuji but that didn't work either.

I managed to come up with an alternative solution though

I placed the function in a timer.

Code:
<script type='text/javascript'> 
setTimeout(ShowField('Field Name'), 100); 
</script>

Works just how I wanted it to :)
 
As long as thing works...

In my last posting, I inherited a typo (onload not on[red]L[/red]oad). It should read like this.
[tt]
<script type='text/javascript'>
window.on[red]l[/red]oad = function() {ShowField('Field Name')};
function ShowField(sFieldName) {
//etc etc... the actual function instructions
}
</script>
[/tt]
In your last solution, I actually am doubtful. Should not it be this?
[tt]
setTimeout([red]"[/red]ShowField('Field Name')[red]"[/red], 100);
[/tt]
Furthermore, 100 ms is ad hoc. It can happen that its loading is not funished yet... I would much prefer to use onload handler.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top