...

View Full Version : Making a script run earlier



PapaGeek
08-06-2012, 10:08 PM
Is there a way to make a JavaScript run before a page is loaded instead of after it is loaded?

Both of my parallel websites, http://www.variablehtml.com (fluid designs) and http://www.variablestyle.com (fixed width designs), have default CSS files which are changed by the VariableHTML JavaScript which is referenced in the body tag “onload” attribute. The pages seem to flash the default CSS rules before changing to the proper ones.

Is there a way to make the script run earlier? Of course I need something that will run reliably on all browsers!

felgall
08-06-2012, 11:29 PM
Placing the script at the bottom of the page and allowing it to run straight away rather than waiting for all the other files to load will get it running as early as it probably can (since it needs the HTML to be loaded first in order to be able to interact with it).

If it doesn't need to interact with the lower part of the HTML then you could move the script to a higher position within the body to get it to run even earlier.

PapaGeek
08-07-2012, 01:59 AM
Thanks, I changed the code at the start of the page from:


<!--#extract end -->
</head>
<body onload="VariableHTML()">
<!--#extract virtual="banner.inc"-->
<div id="framework">

To:


<!--#extract end -->
</head>
<!--#extract virtual="banner.inc"-->
<body>
<script type="text/javascript">
VariableHTML();
</script>
<div id="framework">

I moved the body tag inside the banner include file so that I can make other changes as necessary.

I only did this on the VariableStyle, fixed width, templates so far. It looks like it did solve the issue.

If others could browse to the style website and let me know if they see any delay in the proper display coming up, I’d appreciate it. Also change browser window size and browser to other pages to see if they come up immediately also.

These pages change design based on both the width of the page and also the font size you select.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum