views:

426

answers:

2

Hi All,

I have a page that does the following:

  1. The browser loads a very simple page with a valid head and body, with only a script/noscript pair as content.

  2. In the body, it has a script (script a) that runs a function onLoad. This function dynamically includes a second script (script b), and runs a function in it when it becomes available.

  3. The second script is a .js file that does various work.

Both scripts are parsed by PHP and use the application/x-javascript content type.

Now, I have this all working just fine, except a couple of JS hiccups. JavaScript isn't one of my strong languages, so I'm hoping these are simple issues and somebody can point me in the right direction.

Problem 1: If I do a simple alert('you are in script b'); in the second script, it works as expected. However, if I do anything else, it works fine, and then the browser keeps indicating that it is loading forever. This is the color tween in firefox, or the spinning thing in IE.

I've tried ending the script in different ways, and nothing seems to help. Any idea how to indicate to the browser that the script is all the way loaded? It's a .js file that is forced to parse through PHP.

Problem 2: The second script doesn't seem to be included at all in either Opera or Google Chrome. Works fine in FF/IE, other than the loading issue. Can anyone see if Im using something that isn't compatible in the loading of the second script?

Thanks!

Update:

Thanks for the answers. I actually have firebug, which is why I know everything is working properly (in FF, at least). I don't actually know that the script isn't working in Opera/Chrome, but nothing happens.

It is quite a bit of code =o) I will copy the actual responses out of firebug and post those, so you can see exactly what the code is. As far as the webserver closing the connection, I was thinking that too, but it seems odd that if I make script b into alert('whatever'); it will alert and then stop loading, but it I do everything exactly identical, but make the script document.write('whatever); it will load forever.

Here are the scripts, updated, copied directly from the net tab of firebug:

Note that discoverfire.net is an internal domain, so you won't be able to load anything from there...

Initial HTML page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <title>Welcome!</title>
    <style>body { font-family:arial; }</style>
    <script language="JavaScript" type="text/javascript" src="http://www.discoverfire.net/analytics/l/a.js"&gt;
    </script>
    <script language="JavaScript" type="text/javascript">
     document.onload = Start();
     function Start(){
      TAFKing_version = '1.0';
      TAFKing_lkey = '19-8O-KKA8HV';
      TAFKing_Lander();
     }
    </script>
</head>
<body>
    <noscript>
     Oops!  We can't forward you properly because your JavaScript is turned off.<br /><br />
     <a href='http://www.discoverfire.net/analytics/l/noscript/19-8O-KKA8HV.html'&gt;Please click here to continue.</a>
     <img src='http://www.discoverfire.net/analytics/l/imp/19-8O-KKA8HV.png' border='0' alt='tell a friend' />
    </noscript>
</body>
</html>

** Script A (...a.js): http://www.discoverfire.net/analytics/l/a.js **

function TAFKing_Lander(){

    version = TAFKing_version;
    lkey = TAFKing_lkey;

    var scrb = document.createElement('script');
    scrb.type = 'text/javascript';
    scrb.src = 'http://www.discoverfire.net/analytics/l/b.js?lkey='+lkey+'&amp;version='+version+'&amp;cb=4eohe8e65'
;

    document.getElementsByTagName('head')[0].appendChild(scrb);
    Interval = setInterval("Waiter()", 10);

    return;

}

function Waiter(){
    if(window.TAFKing_LanderB) {
     clearInterval(Interval);
     TAFKing_LanderB();
    }
}

Script B (...b.js): http://www.discoverfire.net/analytics/l/b.js?lkey=19-8O-KKA8HV&amp;version=1.0&amp;cb=4eohe8e65

function TAFKing_LanderB(){
    document.write("there are just a whole bunch of doc.writes here that build a simple table");
}
+1  A: 

I bet it is nothing related with the scripts, but with the webserver. Your description, specially that it affects many browsers, and some of them don't even run the scripts, leads me to believe that the webserver is not closing the connection. Perhaps the webserver is not properly handling HTTP/1.1 Keep-alive requests.

Try using Firebug in Firefox. Install it, enable it for your page, reload the page and check the "Net" tab for what really is keeping the connection open.

Juliano
+1  A: 

This is a lot of code to go through. You should definitely get Firebug to help you diagnose it. The latest version will even show you when/if the onload events occur.

Firebug will also allow you to output message simply by writing console.log('somevar=',var); to test their values. You can even use the console to test value after the page has loaded since you're using the global name space.

Off the top of my head, I would make sure the connection properly closes in php. Also

document.onload = Start();

would assign the result of Start() to onload, not Start which is defined later.

Also window.onload is more compatible/standard.

You may want to save the output of your js files as outputphpA.js and outputphpB.js, directly source those and see if the loading behavior differs. That should help diagnose if it's a php issue.

Keith Bentrup