Forum Moderators: open

Message Too Old, No Replies

I want my Javascript loaded before HTML

         

Habtom

10:05 am on Jun 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is there a way to load a .js file before the whole HTML page loads?

Tnx

Habtom

RonPK

10:07 am on Jun 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Forgive me for asking, but have you tried this?


<html>
<head>
<script type="text/javascript" src="myscript.js"></script>
[rest of page]
</html>

AFAIK, browsers should load and parse the script before continuing with the rest of the page.

Habtom

11:16 am on Jun 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, that is how it is on my page. But it doesn't execute the javascript first, not even in between, but last.

Hab

Habtom

11:16 am on Jun 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, that is how it is on my page. But it doesn't execute the javascript first, not even in between, but last.

Hab

RonPK

11:25 am on Jun 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe the script contains an onload-instruction that would make it run after the page has been loaded?

adb64

2:03 pm on Jun 12, 2006 (gmt 0)

10+ Year Member



I don't think it can be done like you want. The browser will first load the complete HTML page and after that it will scan the received HTML for javascripts, stylesheets, images, etc and request the server for those files.
Maybe a browser starts scanning while the page is being received and start requesting the server for those other files while the HTML is being loaded. But that is something internal for a browser which you can't influence.

If you want the javascript to execute first you could make an HTML file with no actual content apart from loading the javascript and then have the javascript to do what it should do and then load the actual page content.

kaled

2:11 pm on Jun 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



QUESTION
What exactly are you trying to achieve?

Kaled.

Habtom

9:06 am on Jun 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I found a solution for it. Actually, the js was still loading, it was my function on onload event of the body took sometime before it executes (probably waiting for the page to load).

The function was called onload of the body in a format <body onLoad="myfunction();">. I changed it as follows:

I moved the event to the form on (onfocus event) of one of the select boxes.

I put a hidden box to control if the box is already loaded, as it might load again when it gets the focus. The code:

<script language="javascript" type="text/javascript">
function null_load_value() {
document.main.load_value.value = 0;
}
</script>
<script language = "JavaScript" type="text/JavaScript">
function load_once() {
if (document.main.load_value.value == 1) {
document.main.load_value.value = 1;
}
else {
myfunction();document.main.load_value.value = 1;
}
}
</script>

and it works very well. :)

Tnx,
Hab