1

I am trying to work on a keydown event in javascript and I am currently stuck on something that never happened to me before :

var maxL = $("#myField").attr("maxLength")
console.log("Maximum Length : " + maxL);

if (e.key.length === 1 && str.length >= parseInt(maxL)) {
    console.log(">>>> in if");
    console.log(">>>> e.char == \"" + e.key + "\"");
    e.preventDefault();
}

When I load the page, it fails. On debug, my server console logs this error :

Error Parsing /folder/myPage.xhtml: Error Traced[line: 384] The entity name 
              must immediately follow the '&' in the entity reference.

where 384 is the line where the if is

I tried both conditions individually and they work just fine, which leads me to think there is something wrong with the "&&" operator.

If this is of any importance, I work on a WebSphere server in JSF with PrimeFaces and I try to run it in Chrome. The javascript is embedded inside a div in order to delete it after the page is loaded :

<div id="deleteScript">
    <SCRIPT>
        //my script code

        $("#deleteScript").remove();
    </SCRIPT>
</div>
Sirmyself
  • 1,464
  • 1
  • 14
  • 29

4 Answers4

2

I actually had to use the following syntax :

<div id="deleteScript">
    <SCRIPT>
        <!--//--><![CDATA[//><!--
            //my script code

            $("#deleteScript").remove();
        //--><!]]>
    </SCRIPT>
</div>

Which I could find on this other answer. BalusC seems to think that we should only use this solution if we work for older web browsers, but it appears that it becomes necessary when we work on older JSF servers as well.

Sirmyself
  • 1,464
  • 1
  • 14
  • 29
1

Looks like the jsf is interpreting parts of your js as if it was xhtml.

Wrap your script (after the <script> tag) with <![CDATA[ and ]]> to make it interpret the content literally:

<script>
    <![CDATA[
        //your code
    ]]>
</script>

The <![CDATA[ ... ]]> tags are used in XML files to allow the use of characters that usually are reserved such as '&', '>' etc. You can read more on this answer and here.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
0

This is not a problem with the browser; It looks like your server is attempting to operate on the "&&". Maybe you have to escape it in your code.

Aanand Kainth
  • 719
  • 4
  • 18
  • This would have been a better answer if it said _how_ to escape the & signs. And if it mentioned to also look for < signs. – Mr Lister Mar 29 '18 at 17:59
-3

The problem comes from the fact that you have your JS written in the html script tag. What I would do if I were you is:

<div id="deleteScript">
    <script src="path/to/script.js"></script>
</div>

Since the script tag is within the div with ID "deleteScript", the element will exist when the script loads and you can delete it.

TVG
  • 259
  • 3
  • 8
  • Writing a script directly in a ` – Sirmyself Mar 29 '18 at 14:57