4

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 accomplish what I'm trying to do? Thanks!

7
  • <%=myVar.varDefinition.getText()%> This is neither js nor html. 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. Commented Aug 29, 2013 at 15:39
  • Your jsp is obviously the source of the problem. Commented Aug 29, 2013 at 15:42
  • As in I need to clean the string before I call it via JSP? 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. Commented Aug 29, 2013 at 15:57

1 Answer 1

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

I started adding some of this in but I have to run to lunch. I'll let you know how it works out! Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.