How do I call a Javascript function AFTER the page has loaded?
** EDIT **
I am afraid that I am in the wrong direction - the problem is not what I was asking about. Javascript works as it should, it's PHP that doesn't show that I want it on the first "run" - and I don't know why. Sorry for wasting your time ...
**
I have a form that may or may not contain data in its fields. If the user reaches the form and he already has some data in the system, his ID number, for example, will be displayed in the ID field - and the JavaScript onLoad function works will disable the input field and change its style:
<body onload="runFunction()">
the problem is that it seems to work before PHP does its part, and only if I refresh the page the JS function does whatever I want it to do (it disables and styles other form fields that depend on the PHP condition ).
Is there a way to run this JS function AFTER the page has been rendered? I tried to run this before closing the page:
<body>
...
...
<script>
runFunction();
</script>
</body>
</html>
but to no avail.
any ideas? Thanks!
some of the relevant PHP code: [I've removed the id attributes to make it easier to read)
<?php if ($h_phone != '' && $h_phone_pre != '') {
echo "<td class=\"input\"><input type=\"text\" id=\"new_user_home_tel\" value=\"$h_phone\" size=\"8\" maxlength=\"7\" disabled=\"disabled\" /> -
<select id=\"new_user_home_tel_pre\" disabled=\"disabled\">
<option value=\"$h_phone_pre\" selected=\"selected\"></option>";
} else {
echo '<td class="input"><input type="text" id="new_user_home_tel" size="8" maxlength="7" /> -
<select id="new_user_home_tel_pre">
<option value=" " selected="selected"></option>';
}?> <option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="08">08</option>
<option value="09">09</option>
</select>
</td>
the Javascript code just changes the style if the field is not empty and that works, but PHP only works after updating.
a source to share
Your Javascript runs after the page is loaded, the problem is elsewhere.
Your server must be sending different HTML before and after the update. I suggest that if you keep the source of the page, you get the first time and compare it to the source you get after the refresh. I bet you will notice the difference and it will tell you what is wrong.
a source to share
One way is to dump the page buffer in PHP. This means that everything generated happens in memory until the script finishes, and then everything is sent at once.
Take a look at http://uk.php.net/ob_start
a source to share
Non JS library response:
<script>
window.onload = function(){
/* do stuff */
}
</script>
However, using a JS library like JQuery will take care of all cross browser error / issues.
Your right to place blocks <script>
at the end of your body. This improves page loading performance when the browser is blocked when hitting a block <script>
.
a source to share
I know it is very late for this post, but the reason it doesn't work is because PHP renders the HTML and then you can't interact with it again (unless you can do some awesome stuff AJAX). The point is JS works fine bc on the client side, PHP doesn't work bc, it is server side. Hope this helps anyone who comes here!
a source to share