...

View Full Version : createElement creates error in IE6



agauad
07-13-2006, 10:00 PM
Hello,

I'm trying to build a function that includes a javascript or php script into the web page by including a SCRIPT element into the HEAD element.

the code looks like this:

function includeScript(filePath){
var fileExt = filePath.substring(filePath.lastIndexOf("."),filePath.length);
var scriptType;
switch(fileExt){
case "js": scriptType = "text/javascript";
break;
case "php": scriptType = "text/php";
break;
}
var scrElement = document.createElement("script");
scrElement.setAttribute("type",scriptType);
srcElement.setAttribute("src",filePath);
document.documentElement.childNodes[0].appendChild(scrElement);
}

Do someone knows why the browser launchs an error telling that it is unable to open the window......

Thanks and regards

Kor
07-13-2006, 11:26 PM
try:

document.getElementsByTagName('head')[0].appendChild(scrElement);

agauad
07-14-2006, 09:46 PM
Thanks a lot.....
:thumbsup: :)
Now the browser open the document but, I don't know if it compiles the code in the external source......

Kor
07-14-2006, 09:55 PM
how is your code now?

agauad
07-15-2006, 05:26 AM
The code is as follows:

function includeScript(filePath){
var d = document,fileExt = filePath.substring(filePath.lastIndexOf("."),filePath.length);
var scriptType;
switch(fileExt){
case "js": scriptType = "text/javascript";
break;
case "php": scriptType = "text/php";
break;
}
var scriptElement= d.createElement("script");
scriptElement.setAttribute("type",scriptType);
scriptElement.setAttribute("src",filePath);
d.getElementsByTagName("head")[0].appendChild(scriptElement);
}

Now, this is in an external file called IncludeScript.js wich I include by declaring a script element with src pointing to this file. Now oce this is done, I call the function from another script tag and then I run the following function that displays the elements in the Head element.


function displayHeadElements(){
var h = document.getElementsByTagName("head")[0];
document.write("<TABLE>");
for(var i=0;i<h.childNodes.length;i++){
var e = h.childNodes[i];
document.write("<tr colspan=\"2\"><td>ELEMENT</td></tr>");
document.write("<tr><td>NodeName</td><td>NodeType</td>");
document.write("<tr>");
document.write("<td>" + e.nodeName + "</td>");
document.write("<td>" + e.nodeType + "</td>");
document.write("</tr>");
document.write("<tr colspan=\"2\"><td>ATTRIBUTES</td></tr>");
document.write("<tr><td>AttrName</td><td>AttrValue</td></tr>");
for(var j=0;j<e.attributes.length;j++){
var a = e.attributes[j];
if(a.value!=null){
document.write("<tr>");
document.write("<td>" + a.name +"</td>");
document.write("<td>" + a.value + "</td>");
document.write("</tr>");
}
}
}
}

and I call it in the body.
Now, this function list the included "script" element so the function works (thanks to you) but when calling a function defined in the external ".js" file included using the function the Browser throws a "An object was/is expected" error.

If you want to try it.....

Thanks anyway
:thumbsup: :thumbsup: :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum