最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Stop converting "<" to "<" in jsp - Stack Overflow

matteradmin7PV0评论

I have jsp page and some set of javascript code written inside the jsp page.

for(i=0;i<10;i++)
{
//some stuff
}

but in the browser its giving error and the rendered code look like

for(i=0; i&lt;10; i++ { }

how to stop converting "<" to "&lt;".

Thanks in advance.

I have jsp page and some set of javascript code written inside the jsp page.

for(i=0;i<10;i++)
{
//some stuff
}

but in the browser its giving error and the rendered code look like

for(i=0; i&lt;10; i++ { }

how to stop converting "<" to "&lt;".

Thanks in advance.

Share Improve this question asked Jan 31, 2011 at 10:14 Ra.Ra. 9654 gold badges18 silver badges30 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 1

Is your JS code meant to be executed, or just displayed as it is?

If you have the former situation, is your code inside <script type="text/javascript">...</script> tags?

If you have the latter situation, then characters such as < HAVE to be converted to &lt, otherwise they would be read as HTML tags by your browser.

JSP does by default not do that. Aren't you actually using JSTL <c:out> to print JavaScript code? It can namely do that. You could disable that by adding escapeXml="false" attribute.

Anyway, best would always be to put JS code in its own .js file which you then include in the head as follows:

<script src="script.js"></script>

Hope this helps....

function toHtml(myString)
{
    htmlString = myString.split("&lt;").join("<");
    htmlString = htmlString.split("&gt;").join(">");
    htmlString = htmlString.split("&quot;").join("\"");
    htmlString = htmlString.split("&apos;").join("\'");
    return htmlString;
}

Gretting. Víctor

Post a comment

comment list (0)

  1. No comments so far