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!
- <%=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
1 Answer
Reset to default 3You 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
版权声明:本文标题:Passing HTML string to JavaScript function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743999870a2573659.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论