...

View Full Version : IE reloading bugs for dynamically loading JS files



marryat
10-07-2008, 01:50 PM
I am using the good article on updating the DOM to include external javascript files dynamically into the head of a page to load in prototype.js from the google servers. I can get it to work consistently in firefox, and it works in IE if I visit the page from somewhere else, or enter the URL into the address bar and hit enter. However if I force a refresh (ctl F5 or F5) it throws a javascript error stating:


Line: 4027
char: 5
Error: 'null' is null or not an object
Code: 0


This error is triggered at the


headTag.appendChild(scriptTag);

line. Full code included below.



var protoUrl = "ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js"

var protoHost = (("https:" == document.location.protocol) ? "https://" : "http://");

//This function is used to include javascript files into the head section of the page
function includeJavascript(src) {
//Checks if the function includes the correct syntax to include js files
if (document.createElement && document.getElementsByTagName) {
var headTag = document.getElementsByTagName("head")[0];
var scriptTag = document.createElement('script');
scriptTag.setAttribute('type', 'text/javascript');
scriptTag.setAttribute('src', src);
headTag.appendChild(scriptTag);
}
}

//include javascript src file
includeJavascript(protoHost + protoUrl);



I am using IE 7.0.5730.13

Thanks a lot.

vwphillips
10-07-2008, 05:07 PM
test with same version of IE and works for me


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/
var protoUrl = "ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js"

var protoHost = (("https:" == document.location.protocol) ? "https://" : "http://");
function includeJavascript(src) {
//Checks if the function includes the correct syntax to include js files
if (document.createElement && document.getElementsByTagName) {
var headTag = document.getElementsByTagName("head")[0];
var scriptTag = document.createElement('script');
scriptTag.setAttribute('type', 'text/javascript');
scriptTag.setAttribute('src', src);
headTag.appendChild(scriptTag);
alert(headTag.getElementsByTagName('SCRIPT').length)
}
}

/*]]>*/
</script></head>

<body onload="includeJavascript('protoHost + protoUrl');" >

</body>

</html>

marryat
10-08-2008, 10:25 AM
I am trying to run the includeJavascript function before the onload event to ensure that the prototype library is fully loaded before any other code is run. (i.e. AJAX requests in an onload function.)
I put all of that code in an external javascript file, which includes the includeJavascript function call.

any further help would be greatfully received.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum