1

This is how my code gets shown in my developer console (Chrome):

<script type="text/javascript">// <!&#091;CDATA&#091; function hidden(){ document.getElementById("test123").style.visibility = "hidden"; } document.getElementById("test123").addEventListener("click", hidden); var banner =document.querySelectorAll('.contactBanner1'); for(var i=0;i<banner.length;i++){ banner&#091;i&#093;.addEventListener('mouseover',hidden,false); } // &#093;&#093;></script> 

This is the error I get now: Uncaught SyntaxError: Unexpected token ILLEGAL

How do I correctly use the [?

7
  • 1
    Assuming you're entering them as <![CDATA[ & //]]>, it works fine for me upon inspection. You running th eoutput through a parser/encoder of some kind? Commented Mar 10, 2016 at 19:54
  • using an external js file would bypass these document-based encoding issues. Commented Mar 10, 2016 at 20:00
  • @BradChristie Sorry I don't really know what you mean, my wordpress site automatically adds the <![CDATA[ & //]]> and I don't know how to use it. So I ran into this error. Commented Mar 10, 2016 at 20:00
  • why would wordpress (invented 2003) add CDATA trash that hasn't been needed since 1998? Commented Mar 10, 2016 at 20:02
  • I'm creating the normal <script> tag and the next time I open it there automatically is the CDATA.. Commented Mar 10, 2016 at 20:03

1 Answer 1

1

Back when XML was all the rage, people spent a lot of time thinking about their XHTML and XML being parsed by a single XML parser. As such, the XHTML needed to pass XML validation.

But, a problem that can be encountered is when that XHTML includes JavaScript and that JavaScript contains, say, a < meant to mean less-than.

In HTML, XHTML and XML the < means beginning of a start or end tag.

To prevent this, it was recommended that scripts in XHTML have their content marked as Character Data, so that if/when the XML parser got to it, it would effectively ignore special symbols like < (among others).

So, scripts should have been written as:

 <script type="text/javascript"> //<![CDATA[ // Inside the CDATA < gets ignored //]]> </script> 

Your code doesn't follow this, instead you are providing the Unicode characters for [ and ].

And, lastly, if you are not worried about your JavaScript being parsed as XML, you can just write the more modern version:

 <script> </script> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.