admin管理员组文章数量:1315364
I have two functions in my script, one that outputs some long HTML string, the other one that takes this string as a parameter and processes it.
function myFirstFunction() {
//output some HTML
return myHTML;
}
var myHTML = myFirstFunction();
function mySecondFunction(myHTML) {
//do something with parameter
}
For some reason that I can't figure out, the Chrome JS Console keeps giving me the following error: "Uncaught SyntaxError: Unexpected token <"
I thought maybe that was due to the fact, that the outputed HTML was pretty long since it seems to be working with small chunks of HTML. Any thoughts? Thx!
I have two functions in my script, one that outputs some long HTML string, the other one that takes this string as a parameter and processes it.
function myFirstFunction() {
//output some HTML
return myHTML;
}
var myHTML = myFirstFunction();
function mySecondFunction(myHTML) {
//do something with parameter
}
For some reason that I can't figure out, the Chrome JS Console keeps giving me the following error: "Uncaught SyntaxError: Unexpected token <"
I thought maybe that was due to the fact, that the outputed HTML was pretty long since it seems to be working with small chunks of HTML. Any thoughts? Thx!
Share Improve this question asked Sep 19, 2012 at 18:27 sf89sf89 5,2487 gold badges25 silver badges27 bronze badges 4- 1 Works fine here: jsfiddle/utuGY/1 – Mark Pieszak - Trilon.io Commented Sep 19, 2012 at 18:30
- stackoverflow./questions/3143698/… – Santiago Elvira Ramirez Commented Sep 19, 2012 at 18:30
- @ sachleen "myHTML" is some long HTML output. @ mcpDESIGNS yes it does work with short HTML strings. @ Santiago Elvira Ramirez thanks, I'm looking at this answer right now, but it seems that the other person had her script working fine in Firefox, this is not my case. – sf89 Commented Sep 19, 2012 at 18:35
- @sf89 could show myHTML? – Santiago Elvira Ramirez Commented Sep 19, 2012 at 19:54
4 Answers
Reset to default 2Here's problem:
myHTML is a HTML string like this:
var myHTML ="<div id="foo"><div id='hello'>hello</div><div id="bar">bar'asdf'asf"sdf"&soidf;</div></div>";
That won't work because you have quotes and stuff inside it that are NOT escaped.
If you used innerHTML
to get the HTML of an element on the page, this wouldn't be a problem.
myHTML is constructed with some < or > extra so verify the html string
There is a way to pass an html text as a parameter to javascript function: Just replace all specifics chars in html tag before you pass it to javascript function. Later in javascript you just revert all specific chars back to normal. The trick is to cause javascript recognizes only those chars it excepted.
Example:
[[C#]]
string urlString = "http://localhost:8698/DotNetNuke_Community_06.02.01_Install/Default.aspx?TabID=157&categoryId=92&newCategoryId=92";
urlString = urlString.Replace("<", "µ");
urlString = urlString.Replace(">", "Ħ");
urlString = urlString.Replace("&", "€");
urlString = urlString.Replace(":", "¥");
urlString = urlString.Replace("=", "¬");
urlString = urlString.Replace("/", "ä");
urlString = urlString.Replace("?", "¿");
urlString = urlString.Replace("'", "ʅ");
function(urlString);
[[Javascript]]
function(urlString){
urlString = urlString.replace(/µ/g, "<");
urlString = urlString.replace(/Ħ/g, ">");
urlString = urlString.replace(/€/g, "&");
urlString = urlString.replace(/¥/g, ":");
urlString = urlString.replace(/¬/g, "=");
urlString = urlString.replace(/ä/g, "/");
urlString = urlString.replace(/¿/g, "?");
urlString = urlString.replace(/ʅ/g, "'");
}
Regards,
I'm assuming that you're literally trying to pass html, not in a string literal. If you try something like:
myFunction(<html><body>...);
That'll definitely through an error. You need to use string literals:
myFunction("<html><body>...");
If you're using html that has quotes in it, you need to escape them or use single quotes:
"<div id="name">"
is not a valid string. Make it either:
"<div id=\"name\">" or
'<div id="name">'
本文标签: JavaScript functionpass HTML as parameterStack Overflow
版权声明:本文标题:JavaScript function, pass HTML as parameter - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741975278a2408083.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论