Note that there are some explanatory texts on larger screens.

plurals
  1. POJSP passing String attribute with XML characters to JavaScript function not working
    primarykey
    data
    text
    <p>I need to pass a jsp string variable to a javascript function. This is usually fairly simple but I'm currently having issues because the string I'm passing contains xml characters.</p> <p>This is the jsp (shortened for clarity purpose)</p> <pre><code>&lt;%@page language="java" import="java.util.*" pageEncoding="UTF-8"%&gt; &lt;%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"&gt; &lt;body&gt; &lt;input type="button" value="MyButton" onclick="myFunction('&lt;c:out value="${current.xmlOutput}"/&gt;');" /&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>The value in "current.xmlOutput" contains the following</p> <pre><code>&lt;chart caption='Flight Hours' xAxisName='Flight' yAxisName='Hours' &gt;&lt;set label='AC908' value='126' /&gt;&lt;set label='AC812' value='234' /&gt;&lt;set label='AC184' value='78' /&gt;&lt;/chart&gt; </code></pre> <p>The generated JSP source</p> <pre><code>&lt;input type="button" value="MyButton" onclick="myFunction('&amp;lt; chart caption = &amp;#039;Flight Hours&amp;#039; xAxisName = &amp;#039;Flight&amp;#039; yAxisName = &amp;#039;Hours&amp;#039;&amp;gt;&amp;lt; label = &amp;#039;AC908&amp;#039; value = &amp;#039;126&amp;#039; /&amp;gt; &amp;lt; label = &amp;#039;AC812&amp;#039; value = &amp;#039;234&amp;#039; /&amp;gt; &amp;lt; label = &amp;#039;AC184&amp;#039; value = &amp;#039;78&amp;#039; /&amp;gt; &amp;lt;/ chart &amp;gt;');" /&gt; </code></pre> <p>When I press the button it never calls the function. If "current.xmlOutput" contains a simple String like "Test" everything works fine. In the chrome developer tools it give me the following message "Uncaught SyntaxError: Unexpected identifier "</p> <p>Also, I have a filter that does this req.setCharacterEncoding("UTF-8"); Any one know what I'm doing wrong? Any help would be appreciated.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload