Why is CDATA needed and not working everywhere the same way?

precagagce

New Member
In Firefox's and Chrome's consoles, this works (alerts script content):\[code\]var script = document.createElement("script");script.textContent = ( function test() { var a = 1; });document.getElementsByTagName("head")[0].appendChild(script);alert(document.getElementsByTagName("head")[0].lastChild.textContent);\[/code\]Using this code as a Greasemonkey script for Firefox works too.Now, if want to add a "private method" \[code\]do()\[/code\] to \[code\]test()\[/code\] It is not working anymore, in neither Firefox/Chrome console nor in a Greasemonkey script:\[code\]var script = document.createElement("script");script.textContent = ( function test() { var a = 1; var do = function () { var b = 2; }; });document.getElementsByTagName("head")[0].appendChild(script);alert(document.getElementsByTagName("head")[0].lastChild.textContent);\[/code\]To make this work in a Greasemonkey script, I have to put all the code in a \[code\]CDATA\[/code\] tag block:\[code\]var script = document.createElement("script");script.textContent = (<![CDATA[ function test() { var a = 1; var do = function() { var b = 2; }; }]]>);document.getElementsByTagName("head")[0].appendChild(script);alert(document.getElementsByTagName("head")[0].lastChild.textContent);\[/code\]This is only works in a Greasemonkey script; it throws an error from the Firefox/Chrome console. I don't understand why I should use a \[code\]CDATA\[/code\] tag, I have no XML rules to respect here because I'm not using XHTML.To make it work in Firefox console (or Firebug), I need to do put \[code\]CDATA\[/code\] into tags like \[code\]<>\[/code\] and \[code\]</>\[/code\]:\[code\]var script = document.createElement("script");script.textContent = (<><![CDATA[ function test() { var a = 1; var do = function() { var b = 2; }; }]]></>);document.getElementsByTagName("head")[0].appendChild(script);alert(document.getElementsByTagName("head")[0].lastChild.textContent);\[/code\]This doesn't working from the Chrome console. I've tried adding \[code\].toString()\[/code\] at the end like many people are doing (\[code\]]]></>).toString();\[/code\]), but it's useless.I tried to replace \[code\]<>\[/code\] and \[code\]</>\[/code\] with a tag name \[code\]<foo>\[/code\] \[code\]</foo>\[/code\] but that didn't work either.Why doesn't my first code snippet work if I define \[code\]var do = function(){}\[/code\] inside another function? Why should I use \[code\]CDATA\[/code\] as a workaround even if I'm not using XHTML? And why should I add \[code\]<>\[/code\] \[code\]</>\[/code\] for Firefox console if it's working without in a Greasemonkey script?Finally, what is the solution for Chrome and other browsers?EDIT:My bad, I've never used do-while in JS and I've created this example in a simple text editor, so I didn't see "do" was a reserved keyword :pBut problem is still here, I've not initialized the Javascript class in my examples.With this new example, \[code\]CDATA\[/code\] is needed for Greasemonkey, Firefox need \[code\]CDATA\[/code\] between E4X \[code\]<>\[/code\] \[code\]</>\[/code\] and Chrome fails:\[code\]var script = document.createElement("script");script.textContent = (<><![CDATA[var aClass = new AClass();function AClass() { var a = 1; var aPrivateMethod = function() { var b = 2; alert(b); }; this.aPublicMethod = function() { var c = 3; alert(c); };}aClass.aPublicMethod();]]></>);document.getElementsByTagName("head")[0].appendChild(script);\[/code\]Question: why?
 
Back
Top