admin管理员组文章数量:1278854
Forgive me if this is a simple problem but I can't seem to find why this code:
function create_content(c)
{
var html = "<div id='header'>"+c+"</div>";
if(c == "links")
{
var ul = "<ul><li><a href=''>My Link 1</a></li>
<li><a href=''>My Link 2</a></li></ul>";
html = html + ul;
}
return(html);
}
Is giving me this error in Chrome (win):
Uncaught SyntaxError: Unexpected token ILLEGAL
On the line that starts with "var ul = "
Any advice would help thanks!
Forgive me if this is a simple problem but I can't seem to find why this code:
function create_content(c)
{
var html = "<div id='header'>"+c+"</div>";
if(c == "links")
{
var ul = "<ul><li><a href='http://www.mylink.'>My Link 1</a></li>
<li><a href='http://www.mylink2.co.uk'>My Link 2</a></li></ul>";
html = html + ul;
}
return(html);
}
Is giving me this error in Chrome (win):
Uncaught SyntaxError: Unexpected token ILLEGAL
On the line that starts with "var ul = "
Any advice would help thanks!
Share Improve this question edited Feb 20, 2013 at 20:11 Robert Harvey 181k48 gold badges348 silver badges512 bronze badges asked Jan 8, 2011 at 18:37 Doug MolineuxDoug Molineux 12.4k26 gold badges95 silver badges144 bronze badges 1- This question is similar to: How can I assign a multiline string literal to a variable?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – dumbass Commented Jan 22 at 7:01
2 Answers
Reset to default 9You are inserting a line break in your ul
string, between the closing </li>
and the opening <li>
. JavaScript string literals cannot span multiple lines like this by themselves, unless you
Trail a
\
at each line but the last (as Ivo Wetzel says):var ul = "<ul><li><a href='http://www.mylink.'>My Link 1</a></li>\ <li><a href='http://www.mylink2.co.uk'>My Link 2</a></li></ul>";
Break them and concatenate the parts, like this:
var ul = "<ul><li><a href='http://www.mylink.'>My Link 1</a></li>"; ul += "<li><a href='http://www.mylink2.co.uk'>My Link 2</a></li></ul>";
(To keep the newline there you would place a
\n
somewhere, but in HTML it won't matter.)
I found I needed to escape the forward slashes in my closing tags. ie;
<\/script>
or
<\/form>
Then the "Uncaught SyntaxError: Unexpected token ILLEGAL" Error went away and my code processed fine.
本文标签: Javascript Illegal Token ErrorStack Overflow
版权声明:本文标题:Javascript Illegal Token Error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741269887a2369082.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论