Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
    
<%=myVar.varDefinition.getText()%> This is neither js nor html. –  Virus721 Aug 29 '13 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 Aug 29 '13 at 15:39
    
Your jsp is obviously the source of the problem. –  Virus721 Aug 29 '13 at 15:42
    
As in I need to clean the string before I call it via JSP? –  Adam Selene Aug 29 '13 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 Aug 29 '13 at 15:57

1 Answer 1

up vote 1 down vote accepted

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();
}
share|improve this answer
    
I started adding some of this in but I have to run to lunch. I'll let you know how it works out! Thanks. –  Adam Selene Aug 29 '13 at 15:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.