Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6115031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:02:39+00:00 2026-05-23T15:02:39+00:00

Sometimes, I need to render a JavaScript variable using EL in a JSF page.

  • 0

Sometimes, I need to render a JavaScript variable using EL in a JSF page.

E.g.

<script>var foo = '#{bean.foo}';</script>

or

<h:xxx ... onclick="foo('#{bean.foo}')" />

This fails with a JS syntax error when the EL expression evaluates to a string containing JS special characters such as apostrophe and newline. How do I escape it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:02:40+00:00Added an answer on May 23, 2026 at 3:02 pm

    You can use Apache Commons Lang 3.x StringEscapeUtils#escapeEcmaScript() method for this in EL.

    First create a /WEB-INF/functions.taglib.xml which look like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <facelet-taglib 
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
        version="2.0">
        <namespace>http://example.com/functions</namespace>
    
        <function>
            <name>escapeJS</name>
            <function-class>org.apache.commons.lang3.StringEscapeUtils</function-class>
            <function-signature>java.lang.String escapeEcmaScript(java.lang.String)</function-signature>
        </function>
    </taglib>
    

    Then register it in /WEB-INF/web.xml as follows:

    <context-param>
        <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
        <param-value>/WEB-INF/functions.taglib.xml</param-value>
    </context-param>
    

    Then you can use it as follows:

    <html ... xmlns:func="http://example.com/functions">
    ...
    <script>var foo = '#{func:escapeJS(bean.foo)}';</script>
    ...
    <h:xxx ... onclick="foo('#{func:escapeJS(bean.foo)}')" />
    

    Alternatively, if you happen to already use the JSF utility library OmniFaces, then you can also just use its builtin of:escapeJS() function:

    <html ... xmlns:of="http://omnifaces.org/functions">
    ...
    <script>var foo = '#{of:escapeJS(bean.foo)}';</script>
    ...
    <h:xxx ... onclick="foo('#{of:escapeJS(bean.foo)}')" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Entity Framework 4.1 and do sometimes need to call stored procedures.
I'm trying to page scrape some DOM that looks like this: <span>text</span> and sometimes
I need to pass coordinates to javascript from HTML 5 using the data- tag
I sometimes need to pass additional parameters to a page via the URL. I
I sometimes need to use Visual Studio when I have limited screen real estate
I sometimes need to look for information for a special portion of code. When
I sometimes need to debug JS in other browsers, and it would be really
I sometimes need to run gacutil.exe or installutil.exe, etc. from the command line. Is
In my app I sometimes need to rebuild and repopulate database file. SQLite databse
If i iterate over a STL container i sometimes need to know if the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.