joe said the following on 11/20/2006 12:08 PM:
Quote:
Randy Webb schrieb:
>
> Quote:
>Fix your server so it sends a proper script element. language is
>deprecated, use the type attribute instead.
| >
ok. now the server sends:
<script type="text/javascript">
alert(Executed");
</script>"
|
I hope that was a typo as the alert doesn't have an opening " and you
have an errant " after the script tag:
<script type="text/javascript">
alert("Executed");
</script>
Quote:
Quote:
>which should be included in the div-block.
>>
>And it gets included. Alert the innerHTML and it is there.
| >
how?
|
In FF2.0 and Opera 9, you can alert
document.getElementById('someDiv').innerHTML and see the code you just
put into it. With IE7, it seems that if the only thing going in is a
script block then it blows up and forgets about it. See the test page at
the bottom, along with a solution.
Quote:
Quote:
>Script elements that are inserted via innerHTML do not, with two
>exceptions[1], get executed. You will have to parse out the script block
>itself, and then use createElement to create a script block. Basically, you would find
>the script elements in your HTMLFragment, then call a function like this:
| > Quote:
>function executeJSCode(stringToExecute){
>var newScript = document.createElement('script');
>newScript.type = "text/javascript";
>newScript.text = stringToExecute;
>document.getElementsByTagName('head')[0].appendChild(newScript);
>}
>>
>If you absolutely need it in the div element, change it to
>document.getElementById('someDiv').appendChild(ne wScript)
>>
>And call it as such:
>>
>executeJSCode('String of code here without script tags')
| >
its not invoked, and i would be very surprised if it would, cause its a
javascript function like alert.
|
Did you test it with a valid script statement? The above code works.
Even in IE. The problem comes elsewhere trying to read those script
elements. I didn't post code to do it (I did this time) to "parse out
the script block itself".
Create a test page:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd">
<html>
<head>
<title>Executing Script blocks inserted via innerHTML</title>
<script type="text/javascript">
function loadHTMLFragment(someFragment){
document.getElementById('myDiv').innerHTML = '<br>' + someFragment;
//notes below on that br element
var d = document.getElementById('myDiv').getElementsByTagN ame("script");
var t = d.length;
for (var x=0;x<t;x++){;
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.text = d[x].text;
document.getElementById('myDiv').appendChild(newSc ript);
}
}
</script>
<body>
<div id="myDiv"></div>
<textarea onchange="loadHTMLFragment(this.value)" rows="15"
cols="80"></textarea>
</body>
</html>
Open that page and paste this snippet into the textarea:
<script type="text/javascript">
alert("Executed");
</script>
And then blur the text area. In Opera9 and FF2.0 you get that alert.
In IE7, it seems that if the very first element is a <scriptelement,
then it won't execute it and gives an error that .text is undefined.
But, adding a second <scriptblock it works and executes both. Adding
any other HTML element before the <scriptblock makes it work
flawlessly whether there is one or more script elements. So, I added
that leading <brtag in the code above and it seems to have satisfied IE.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -
http://jibbering.com/faq
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/