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.
我的脚本中有两个函数,一个输出一些长HTML字符串,另一个将此字符串作为参数并处理它。
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 <"
由于某种原因,我无法弄清楚,Chrome JS控制台不断给我以下错误:“未捕获的SyntaxError:意外的令牌<”
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!
我想也许这可能是由于这样一个事实,即输出的HTML很长,因为它似乎与小块的HTML一起工作。有什么想法吗?谢谢!
4 个解决方案
#1
3
Here's problem:
myHTML is a HTML string like this:
myHTML是一个像这样的HTML字符串:
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.
如果您使用innerHTML来获取页面上元素的HTML,那么这不会成为问题。
#2
2
myHTML is constructed with some < or > extra so verify the html string
myHTML由一些 <或> 额外构造,因此验证html字符串
#3
0
I'm assuming that you're literally trying to pass html, not in a string literal. If you try something like:
我假设你真的试图传递html,而不是字符串文字。如果你尝试这样的事情:
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:
如果你使用的是带引号的html,你需要转义它们或使用单引号:
"<div id="name">"
is not a valid string. Make it either:
不是有效的字符串。制作它:
"<div id=\"name\">" or
'<div id="name">'
#4
0
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.
有一种方法可以将html文本作为参数传递给javascript函数:在将其传递给javascript函数之前,只需替换html标记中的所有细节字符。稍后在javascript中,您只需将所有特定字符恢复正常即可。诀窍是使javascript只识别它排除的那些字符。
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,
#1
3
Here's problem:
myHTML is a HTML string like this:
myHTML是一个像这样的HTML字符串:
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.
如果您使用innerHTML来获取页面上元素的HTML,那么这不会成为问题。
#2
2
myHTML is constructed with some < or > extra so verify the html string
myHTML由一些 <或> 额外构造,因此验证html字符串
#3
0
I'm assuming that you're literally trying to pass html, not in a string literal. If you try something like:
我假设你真的试图传递html,而不是字符串文字。如果你尝试这样的事情:
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:
如果你使用的是带引号的html,你需要转义它们或使用单引号:
"<div id="name">"
is not a valid string. Make it either:
不是有效的字符串。制作它:
"<div id=\"name\">" or
'<div id="name">'
#4
0
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.
有一种方法可以将html文本作为参数传递给javascript函数:在将其传递给javascript函数之前,只需替换html标记中的所有细节字符。稍后在javascript中,您只需将所有特定字符恢复正常即可。诀窍是使javascript只识别它排除的那些字符。
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,