Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run JavaScript code before page load?

I have working in asp.net web application. Here I need to run JavaScript before page load.

I have tried:

<body oninit="funinit();" onprerender="funRender();" onload="funload();">   </body>   <script type="text/javascript" language="javascript">     function funinit() {         alert("funinit");     }     function funload() {         alert("funload");     }     function funRender() {         alert("funRender");     }        </script> 

here only funload() is working.

like image 692
Yashwant Kumar Sahu Avatar asked Aug 04 '11 15:08

Yashwant Kumar Sahu


2 Answers

You can use window.onpaint for such purpose like :

<script type="text/javascript">     function preloadFunc()     {         alert("PreLoad");     }     window.onpaint = preloadFunc(); </script> 

I hope it helps you....

like image 77
Pushpendra Avatar answered Sep 23 '22 17:09

Pushpendra


Just inline it?

<script type='text/javascript'> alert("funload"); </script> 

Or put it in a function and call it immediately. Try to put it to the topmost of your page, however since the DOM isnt loaded yet you cant get any other elements.

What is it you want to do?

like image 42
TJHeuvel Avatar answered Sep 22 '22 17:09

TJHeuvel