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.

+1


a source to share


9 replies


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 which doesn't show that I want it on the first "run" - and I'm not sure why. Sorry for wasting your time ...



0


a source


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.

+5


a source


JQuery does this. Anything inside the following will be executed after the page has been rendered:

$(document).ready(function() {
  //Call your function here
});

      

+2


a source


Try the defer attribute.

0


a source


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

0


a source


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>

.

0


a source


Definitely a hack, but you can always place a 2x2 transparent image under your form and run the function using its onLoad. I did a similar thing on a conditionally included file, so there was no error if the file was not included.

0


a source


I would suggest that you add your content to the output variable and than echo at the end of the file. Example:

...
$output .= '<body onload="runFunction();">';
while(...)
{
   $output .= '...';
}
$output .= '</body>';
...
echo $output;

      

0


a source


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!

0


a source







All Articles