In this thread:
<URL:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/...3f42490>
The problem was, and still is, that IE - no version - will allow you to
use createTextNode and then appendChild it to a Script element. It
throws a "Unexpected call to method or property access" error.
The only browsers, that I am aware of, that don't support createTextNode
on a SCRIPT element are IE/Win, IE/MAC, and iCAB3.0.3 (Is there a newer
iCAB?).
iCab and IE/mac support neither .text or .createTextNode so for this
code they have become part of the "Give up Randy, it won't work there"
group.
The browsers, again that I know of, that don't support the .text
property are:
IE/Mac
iCab3.0.3
Safari 1.3.2
Safari 2.0.4
Shiira 1.2.2
Sunrise 0.89
Safari surprised me on windows though as it supports .text, go figure.
Where it left me was wanting to use .createTextNode for support and then
falling back on the .text property.
What I ended up with is this:
var isIE = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 4)
var isIE = true;
@end @*/
function executeJSString(stringToExecute){
var newScript = document.createElement('script');
newScript.type = "text/javascript";
if (isIE)
{
newScript.text = stringToExecute;
}
else
{
var s = document.createTextNode(stringToExecute);
newScript.appendChild(s);
}
document.getElementById("scriptDiv").appendChild(newScript);
}
Where "scriptDiv" is a container for script elements. The actual code
has code in it to empty the container so that I don't end up with
duplicate script blocks and/or excessive script elements.
The problem comes in if a browser, other than IE, supports .text and not
..createTextNode (or errors on it like IE does). That lead me to try to
set the .text property of a script element (on page load) to set a
variable and then branch on that variable. The problem there comes in
when the browser throws an error trying to set the .text property.
function insertScript(scriptContents) {
var useIt = false;
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.text = "var useText=true";
document.getElementById("myDiv").appendChild(newScript);
//end text insert section
var newScript = document.createElement('script');
newScript.type = "text/javascript";
if(useText){
newScript.text = scriptContents;
}
else{
var s = document.createTextNode(scriptContents);
newScript.appendChild(s);
}
document.getElementById("scriptDiv").appendChild(newScript);
}
I don't have a browser that doesn't support .text so I can't test it
thoroughly.
If anybody has any other ideas on a true feature detection approach
and/or that can test the above code on any non-windows browser I would
be gratefully appreciative.
Also, anybody that can test this page with a browser not listed (Other
than Netscape 4.xx series and lower) and give me the results of the 5
button clicks, I would again be gratefully appreciative.
<URL:
http://members.aol.com/_ht_a/hikksnotathome/loadJSFile/>
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -
http://jibbering.com/faq/index.html
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/