admin管理员组

文章数量:1356304

I have a value that I'm currently accessing and passing to a JavaScript function through an onclick.

<a href="#" onclick="openTextWindow('<%=myVar.varDefinition.getText()%>');">Text</a>

An example value that I'd receive from the getText method is shown below.

<h1>My Header</h1><br />My text

This value is then passed to my openTextWindow method.

function openTextWindow(inText) {
    myWindow = window.open('','','width=500, height=300');
    myWindow.document.write(inText);
    myWindow.focus();
}

For some reason, the value stored in inText doesn't match the string with HTML tags that I showed above. It ends up looking like this.

"<lt/>h1<gt/>My Header<lt/>/h1<gt/><lt/>br /<gt/>My text

When inText is written to myWindow, I want that new window to render with the text My Header within a styled header and My text on a line below that. I've tried replacing and escaping characters with no luck. Any ideas on how to fix this or a better way to acplish what I'm trying to do? Thanks!

I have a value that I'm currently accessing and passing to a JavaScript function through an onclick.

<a href="#" onclick="openTextWindow('<%=myVar.varDefinition.getText()%>');">Text</a>

An example value that I'd receive from the getText method is shown below.

<h1>My Header</h1><br />My text

This value is then passed to my openTextWindow method.

function openTextWindow(inText) {
    myWindow = window.open('','','width=500, height=300');
    myWindow.document.write(inText);
    myWindow.focus();
}

For some reason, the value stored in inText doesn't match the string with HTML tags that I showed above. It ends up looking like this.

"<lt/>h1<gt/>My Header<lt/>/h1<gt/><lt/>br /<gt/>My text

When inText is written to myWindow, I want that new window to render with the text My Header within a styled header and My text on a line below that. I've tried replacing and escaping characters with no luck. Any ideas on how to fix this or a better way to acplish what I'm trying to do? Thanks!

Share Improve this question asked Aug 29, 2013 at 15:34 Adam SeleneAdam Selene 431 gold badge1 silver badge3 bronze badges 7
  • <%=myVar.varDefinition.getText()%> This is neither js nor html. – Virus721 Commented Aug 29, 2013 at 15:37
  • Sorry, I figured this was a problem that would need to be solved in JS so I didn't include JSP in my tags. – Adam Selene Commented Aug 29, 2013 at 15:39
  • Your jsp is obviously the source of the problem. – Virus721 Commented Aug 29, 2013 at 15:42
  • As in I need to clean the string before I call it via JSP? – Adam Selene Commented Aug 29, 2013 at 15:48
  • I have no idea what <%=myVar.varDefinition.getText()%> is supposed to be replaced by, because i don't know jsp, and because people who do probably need more context informations. – Virus721 Commented Aug 29, 2013 at 15:57
 |  Show 2 more ments

1 Answer 1

Reset to default 3

You can stash your HTML in a hidden DIV or textarea, then grab that from your function instead of trying to pass it inline.

<a href="#" onclick="openTextWindow('DIV1');">Text</a>
<div id="DIV1" style="display:none"><%=myVar.varDefinition.getText()%></div>

JS:

function openTextWindow(divName) {
    var inText = document.getElemenyById(divName).innerHTML;
    myWindow = window.open('','','width=500, height=300');
    myWindow.document.write(inText);
    myWindow.focus();
}

本文标签: Passing HTML string to JavaScript functionStack Overflow