-2

I got a problem with some javascript code that I cant find a solution to and hope some of you can help me find a solution.

The problem seems to be with the if statment in the code below that says if(a>b). I get different errors depending on how I write the if statement end depending on where I look I get different errors.

Chrome's developer tool says:

Uncaught SyntaxError: Unexpected token ;

IE's developer tool says:

Uncaught SyntaxError: Unexpected token )

I get the same result if i write if(3>1) so i don't Think its a or b that is the problem.

If i try if(b<a) then glassfish throws an exception saying the error below where line 39 is the if statement

Error Traced[line: 39] The content of elements must consist of well-formed character data or markup.

if(1<3) produces the same result as if(b<a)

now if i write if(3===1) or if(3===3) it works fine and the code runs perfectly and produces the expected result for using "===" signs, but now I need to be able to use < or >.

This code worked fine before upgrading glassfish to 4.1 and a lot of the packages to the latest version.

I have recompiled it many times and restarted the server but so far no success.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:cc="http://java.sun.com/jsf/composite" xmlns:tp="http://java.sun.com/jsf/composite/components" xmlns:fn="http://java.sun.com/jsp/jstl/functions">
<cc:interface>
  <cc:attribute name="text" type="java.lang.String" required="true" />
  <cc:attribute name="maxsize" type="java.lang.Integer" required="true" />
  <cc:attribute name="style" type="java.lang.String" required="false" />
</cc:interface>
<cc:implementation>

  <p id="par#{component.clientId}" style="#{cc.attrs.style}">
    <span id="span#{component.clientId}"><h:outputText value="#{cc.attrs.text}" /></span>
  </p>



  <script>
    var span = $("[id='span#{component.clientId}']");

    var linkShow = $("<a/>");
    linkShow.attr("id", "linkShow#{component.clientId}");
    linkShow.attr("style", "color: #07A0DD;");
    linkShow.text("Visa mer");
    linkShow.hide();

    var linkHide = $("<a/>");
    linkHide.attr("id", "linkHide#{component.clientId}");
    linkHide.attr("style", "color: #07A0DD;");
    linkHide.text("Visa mindre");
    linkHide.hide();

    var a = "#{cc.attrs.text}".length;
    var b = #{cc.attrs.maxsize};
    if (a > b) {
      span.text("#{cc.attrs.text}".substring(0, # {
        cc.attrs.maxsize
      }) + "... ");
      $("[id='par#{component.clientId}']").append(span);
      $("[id='par#{component.clientId}']").append(linkShow);
      $("[id='par#{component.clientId}']").append(linkHide);


      $("[id='linkShow#{component.clientId}']").show();
      $("[id='linkShow#{component.clientId}']").click(function() {
        $("[id='span#{component.clientId}']").text("#{cc.attrs.text}");
        $("[id='linkHide#{component.clientId}']").show();
        $("[id='linkShow#{component.clientId}']").hide();
      });

      $("[id='linkHide#{component.clientId}']").click(function() {
        $("[id='span#{component.clientId}']").text("#{cc.attrs.text}".substring(0, # { cc.attrs.maxsize}) + "... ");
        $("[id='linkShow#{component.clientId}']").show();
        $("[id='linkHide#{component.clientId}']").hide();
      });
    }
  </script>
</cc:implementation>

</html>
Thomas_F
  • 11
  • 3
  • 5
    `var b = # {` ??? Please open you console and debug it. Don't ask question on SO just for syntax error. **EDIT:** Ok i see this is relevant to a template engine, so at least use relevant tag in question – A. Wolff Jan 02 '15 at 17:23
  • The debugger will usually also give you the line that causes the error, check that line. – illinoistim Jan 02 '15 at 17:25
  • Ive checked the debug and both a and b gives the correct values where in the case i tested a gave 1006 and b was 200. As mentiond in the comment above ive replaced a and b with 3 and 1 and it stil produces the same result. – Thomas_F Jan 02 '15 at 17:28
  • `;` is standard JavaScript. I guess the `var b = # {` is just a copy-paste error? – unwichtich Jan 02 '15 at 17:34
  • 1
    If it works fine without `<` or `>`, isn't your problem in your template engine with not managing those symbols in xml ? You should paste the ouput in html, not your template in xml. – topheman Jan 02 '15 at 17:38

1 Answers1

1

Solved the problem. As commented the problem was with the template Engine that it wasn't able to understand some of the operators. By putting it in a cdata filed i solved the problem.

Solution:

<script type="text/javascript">
    //<![CDATA[

        var span = $("[id='span#{component.clientId}']");

        var linkShow = $("<a/>");
        linkShow.attr("id", "linkShow#{component.clientId}");
        linkShow.attr("style", "color: #07A0DD;");
        linkShow.text("Visa mer");
        linkShow.hide();

        var linkHide = $("<a/>");
        linkHide.attr("id", "linkHide#{component.clientId}");
        linkHide.attr("style", "color: #07A0DD;");
        linkHide.text("Visa mindre");
        linkHide.hide();

        var a = "#{cc.attrs.text}".length;
        var b = #{cc.attrs.maxsize};
        if (a > b)
        {
            span.text("#{cc.attrs.text}".substring(0,#{cc.attrs.maxsize}) + "... ");
            $("[id='par#{component.clientId}']").append(span);
            $("[id='par#{component.clientId}']").append(linkShow);
            $("[id='par#{component.clientId}']").append(linkHide);


            $("[id='linkShow#{component.clientId}']").show();
            $("[id='linkShow#{component.clientId}']").click(function() {
                $("[id='span#{component.clientId}']").text("#{cc.attrs.text}");
                $("[id='linkHide#{component.clientId}']").show();
                $("[id='linkShow#{component.clientId}']").hide();
            });

            $("[id='linkHide#{component.clientId}']").click(function() {
                $("[id='span#{component.clientId}']").text("#{cc.attrs.text}".substring(0,#{cc.attrs.maxsize}) + "... ");
                $("[id='linkShow#{component.clientId}']").show();
                $("[id='linkHide#{component.clientId}']").hide();
            });
        }
       // ]]>
    </script>
Thomas_F
  • 11
  • 3