Search code examples
javascriptajaxjsfmojarrajsf-2.3

JSF 2.3.14: Syntax error after (re-) rendering a JavaScript code block with constants


The following code fragment generates a javascript syntax error when I push the h:commandButton:

<h:outputScript>
  'use strict';
  const label = '*';
</h:outputScript>
<h:form>
  <h:commandButton>
    <f:ajax render="@all" />
  </h:commandButton>
</h:form>

SyntaxError: redeclaration of const label

The problem is the const label = '*'. How to deal with that in this case?

Mojarra 2.3.14


Solution

  • You can make the h:outputScript conditional, based on facesContext.postback. If you use rendered="#{not facesContext.postback}" it will only be rendered in the initial request and not with Ajax requests:

    <h:outputScript rendered="#{not facesContext.postback}">
      'use strict';
      const label = '*';
    </h:outputScript>
    <h:form>
      <h:commandButton>
        <f:ajax render="@all" />
      </h:commandButton>
    </h:form>
    

    See also: