Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  9] [ 7]  / answers: 1 / hits: 38482  / 13 Years ago, sun, august 21, 2011, 12:00:00

Is there any event handler before onLoad/onPageShow? The trouble with onLoad is if there is any change in display, the page will show up without change until it is fully loaded, then the script will run. What is the best way to make sure it will run as soon as possible?


More From » onload

 Answers
22

If you put Javascript statements (rather than function definitions) inside a <script> tag, they will be executed during page loading - before the onLoad event is fired.



 <html>
<body>
<h2>First header</h2>
<script type=text/javascript>
alert(Hi, I am here);
document.write(<h3>This is Javascript generated</h3>);
</script>
<h2>Second header</h2>
</body>
</html>


The caveat is that you cannot search for elements by ID because these element might not have been rendered yet, so your ability to change the page that way is limited.



Bottom line: possible, not recommended.



What I usually do in such situations is as follows:




  • Make the parts of the page that may change invisible (via style=visibility:hidden;);

  • Make onLoad run a Javascript code that changes the page and then sets the visibility of the said parts to visibility:visible.


[#90506] Friday, August 19, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lizet

Total Points: 479
Total Questions: 96
Total Answers: 113

Location: Mexico
Member since Sun, Jul 25, 2021
3 Years ago
;