Can't use && in view file JavaScript (SOLVED)

Enonic version: 6.0.0
OS: Mac OSX

I have a page component view file with some JavaScript in it. I get an error with “&&”. I get a different error with “&&” so it seems I cannot use “and” operator.

  if( width >= 320 &amp;&amp; width <=  599 ){
      device = 'phone';
  }

Caused by: org.xml.sax.SAXParseException: The entity name must immediately follow the ‘&’ in the entity reference.

  if( width >= 320 && width <=  599 ){
      device = 'phone';
  }

Caused by: org.xml.sax.SAXParseException: The content of elements must consist of well-formed character data or markup.

If I use &lt; then the page loads, but I get a JavaScript error in the browser.

  if( width &gt;= 320 &amp;&amp; width &lt;=  599 ){
      device = 'phone';
  }

Uncaught SyntaxError: Unexpected token ;

All of the error messages above point to the first line of the “if” statement.

1 Like

I solved it by wrapping the JavaScript with //<![CDATA[ //]]> and removing the entities.

Maybe this post will save somebody else some time.

1 Like

Assume you’re doing xslt here Michael? Isn’t there an <xsl:script/> tag too?

No, this is in Thymeleaf HTML file inside the .