Re: createTextNode and IE7
Peter Michaux said the following on 12/1/2006 11:27 AM:
No, it doesn't support window.opera but it has all the rest of your
nightmares :)
>
I was doing this weeks ago :) You even saw a page of mine that did
exactly this. Remember that "insert code" thing I had for code
examples.
Yes, this thread:
<URL:
http://groups-beta.google.com/group/comp.lang.javas cript/browse_thread/thread/499fb07a38e7107 7/3b313bfc2c0090e 6?lnk=gst&q=cre ateTextNode+saf ari&rnum=3#3b31 3bfc2c0090e6>
Was the one I remembered seeing that had the code I used in it and where
I got the idea from that caused this thread to be created.
Below is the code I used to determine if the page is capable
Looks like a convoluted way of doing this:
function insertScript(sc riptContents) {
var useIt = false;
var testScript = document.create Element('script ');
testScript.type = "text/javascript";
testScript.text = "var useText=true";
document.getEle mentById("myDiv ").appendChild( testScript);
var newScript = document.create Element('script ');
newScript.type = "text/javascript";
if(useText)
{
newScript.text = scriptContents;
}
else
{
//Opera 9 falls through to this branch.
var s = document.create TextNode(script Contents);
newScript.appen dChild(s);
}
document.getEle mentById("myDiv ").appendChild( newScript);
}
Although the only browser I know of that won't use createTextNode is IE.
What does that code do in Safari (in fact, any mac browser) when called
with a insertScript('a lert("It worked")') ?
>
Just because text property doesn't work, it doesn't mean that
createTextNode will work. It is just as easy to test both as test one.
It's possible, but unless I see one, or hear of one, I will leave it as
simple as possible which is the goal.
>
I just retested the above coded with success in Mac/Safari 2, Mac/Opera
9, Mac/Firefox 1.5, Win/IE 5.5, Win/IE 6, Win/Netscape 6.
What do the Mac browsers (and even NS6 Win) do with the code above I posted?
I don't use XHR so this is mostly an academic exercise for me as I use
..js files and dynamically load them on the fly.
Most JS programmers don't understand how to control eval and when to/not
to use it though. The major drawback to eval here is the scope chain. I
haven't decided on an attempted course to try to deal with the scope
issue yet but I have some ideas.
Without a doubt.
I don't know that I can make mine work but I won't stop trying to :) I
have been modifying the loadJSFile function for about 5 years now :)
--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Peter Michaux said the following on 12/1/2006 11:27 AM:
Hi Randy,
>
Randy Webb wrote:
>
What is "AIUI"?
>
Randy Webb wrote:
>When running this code in IE7:
>>
>function insertScript() {
> var newScript = document.create Element('script ');
> newScript.type = "text/javascript";
> var s = document.create TextNode("alert ('hi');");
> newScript.appen dChild(s); // problem line
> document.getEle mentById("myDiv ").appendChild( newScript);
>}
>>
>window.onload= insertScript;
>>
>I get this error:
>>
>Unexpected call to method or property access
>>
>And a pointer that points to the newScript.appen dChild(s) line.
>>
>Am I using createTextNode incorrectly or is IE7 getting it wrong?
>>
>The function, as written, works correctly in FF2.0, Opera9 and AIUI,
>>
>function insertScript() {
> var newScript = document.create Element('script ');
> newScript.type = "text/javascript";
> var s = document.create TextNode("alert ('hi');");
> newScript.appen dChild(s); // problem line
> document.getEle mentById("myDiv ").appendChild( newScript);
>}
>>
>window.onload= insertScript;
>>
>I get this error:
>>
>Unexpected call to method or property access
>>
>And a pointer that points to the newScript.appen dChild(s) line.
>>
>Am I using createTextNode incorrectly or is IE7 getting it wrong?
>>
>The function, as written, works correctly in FF2.0, Opera9 and AIUI,
What is "AIUI"?
nightmares :)
>Safari1.3.2 Its an attempt to get around Safari not supporting the
>setting of the .text property of a script element. If IE7 simply won't
>create the text and append it then feature testing for createTextNode
>won't work. So, I came up with the idea of attempting to set the .text
>property with a variable definition then reading that variable.
>setting of the .text property of a script element. If IE7 simply won't
>create the text and append it then feature testing for createTextNode
>won't work. So, I came up with the idea of attempting to set the .text
>property with a variable definition then reading that variable.
I was doing this weeks ago :) You even saw a page of mine that did
exactly this. Remember that "insert code" thing I had for code
examples.
<URL:
http://groups-beta.google.com/group/comp.lang.javas cript/browse_thread/thread/499fb07a38e7107 7/3b313bfc2c0090e 6?lnk=gst&q=cre ateTextNode+saf ari&rnum=3#3b31 3bfc2c0090e6>
Was the one I remembered seeing that had the code I used in it and where
I got the idea from that caused this thread to be created.
Below is the code I used to determine if the page is capable
of inserting scripts with your technique plus the necessary option for
Safari. I try the IE method first because as far as i remember it
doesn't error in Safari but trying them in the other order does error
in IE. However I did put the tests in try-catch blocks for good (or
bad) measure.
>
function newInserter() {
>
var hooks = [
function(script , code) { // IE
script.text = code;
},
function(script , code) { // Safari
code = document.create TextNode(code);
script.appendCh ild(code);
}
];
>
var hook = null;
function insertExampleCo de(code) {
var script = document.create Element('script ');
script.type = 'text/javascript';
hook(script, code);
document.body.a ppendChild(scri pt);
}
>
var testCode = "var testInsertion={ b:3};"
for (var i=0; i<hooks.length ; i++) {
hook = hooks[i];
try {
insertExampleCo de(testCode);
if (testInsertion && testInsertion.b === 3) {
return insertExampleCo de;
}
} catch (e) {}
}
return null;
}
Safari. I try the IE method first because as far as i remember it
doesn't error in Safari but trying them in the other order does error
in IE. However I did put the tests in try-catch blocks for good (or
bad) measure.
>
function newInserter() {
>
var hooks = [
function(script , code) { // IE
script.text = code;
},
function(script , code) { // Safari
code = document.create TextNode(code);
script.appendCh ild(code);
}
];
>
var hook = null;
function insertExampleCo de(code) {
var script = document.create Element('script ');
script.type = 'text/javascript';
hook(script, code);
document.body.a ppendChild(scri pt);
}
>
var testCode = "var testInsertion={ b:3};"
for (var i=0; i<hooks.length ; i++) {
hook = hooks[i];
try {
insertExampleCo de(testCode);
if (testInsertion && testInsertion.b === 3) {
return insertExampleCo de;
}
} catch (e) {}
}
return null;
}
Looks like a convoluted way of doing this:
function insertScript(sc riptContents) {
var useIt = false;
var testScript = document.create Element('script ');
testScript.type = "text/javascript";
testScript.text = "var useText=true";
document.getEle mentById("myDiv ").appendChild( testScript);
var newScript = document.create Element('script ');
newScript.type = "text/javascript";
if(useText)
{
newScript.text = scriptContents;
}
else
{
//Opera 9 falls through to this branch.
var s = document.create TextNode(script Contents);
newScript.appen dChild(s);
}
document.getEle mentById("myDiv ").appendChild( newScript);
}
Although the only browser I know of that won't use createTextNode is IE.
What does that code do in Safari (in fact, any mac browser) when called
with a insertScript('a lert("It worked")') ?
>If it is
>set, then use the .text property. If it isn't set, then use
>createTextNode .
>set, then use the .text property. If it isn't set, then use
>createTextNode .
Just because text property doesn't work, it doesn't mean that
createTextNode will work. It is just as easy to test both as test one.
simple as possible which is the goal.
>Not sure how reliable it is so I thought about using an
>IE conditional to isolate IE and go based on that but it reeks of
>browser detection.
>IE conditional to isolate IE and go based on that but it reeks of
>browser detection.
I just retested the above coded with success in Mac/Safari 2, Mac/Opera
9, Mac/Firefox 1.5, Win/IE 5.5, Win/IE 6, Win/Netscape 6.
Clearly there is appeal to your technique which is why I played with it
until I found the Safari problem. Then I started to get nervous that
some other browser might be able to do XHR but the script blocks
wouldn't run.
until I found the Safari problem. Then I started to get nervous that
some other browser might be able to do XHR but the script blocks
wouldn't run.
..js files and dynamically load them on the fly.
The advantage of using eval() is that as long as the programmer knows
how the code has to be written then it is extremely likely that the
browser will be able to run the scripts.
how the code has to be written then it is extremely likely that the
browser will be able to run the scripts.
to use it though. The major drawback to eval here is the scope chain. I
haven't decided on an attempted course to try to deal with the scope
issue yet but I have some ideas.
Success is the most important part.
If the script blocks are written with care then they could also
run if your technique is proven to work and the use of eval is changed
to your technique.
run if your technique is proven to work and the use of eval is changed
to your technique.
have been modifying the loadJSFile function for about 5 years now :)
--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Comment