admin管理员组文章数量:1403493
Trying to use the following code in a page test.html:
<script language = "javascript">
function test()
{
if (typeof a != "undefined")
{
document.body.innerHTML = "";
document.write(a);
}
else
{
document.body.innerHTML = "";
document.write("a is undefined");
}
var a = "a is defined";
document.write("<br><br>");
document.write("<a href='javascript:void(0)' onclick='function(){ test(a); }'>test</a>");
}
window.onload = function(){ test(); }
</script>
Results in the error "Uncaught SyntaxError: Unexpected token (". How would I get the link to clear the page and display the corresponding variable?
Trying to use the following code in a page test.html:
<script language = "javascript">
function test()
{
if (typeof a != "undefined")
{
document.body.innerHTML = "";
document.write(a);
}
else
{
document.body.innerHTML = "";
document.write("a is undefined");
}
var a = "a is defined";
document.write("<br><br>");
document.write("<a href='javascript:void(0)' onclick='function(){ test(a); }'>test</a>");
}
window.onload = function(){ test(); }
</script>
Results in the error "Uncaught SyntaxError: Unexpected token (". How would I get the link to clear the page and display the corresponding variable?
Share Improve this question edited Oct 20, 2011 at 7:04 Prince John Wesley 63.7k12 gold badges90 silver badges95 bronze badges asked Oct 20, 2011 at 7:03 SystemErrorSystemError 872 gold badges2 silver badges8 bronze badges 7- does it change when you omit the spaces between language="javascript" ? – Andreas Commented Oct 20, 2011 at 7:06
- The problem with his code (actually, one of it - not the syntax error) is that the function inside the string does not create a closure. – ThiefMaster Commented Oct 20, 2011 at 7:07
- Which line is shown to contain the error? – Zirak Commented Oct 20, 2011 at 7:07
- Hi Andreas, if you mean changing language = "javascript" to language="javascript" then no, nothing changes. SE – SystemError Commented Oct 20, 2011 at 7:09
- I don't see your error, but I also don't see the function getting executed at all (adding an alert to the beginning of the script does nothing.) So I suspect the problem to be at the last line. – Madara's Ghost Commented Oct 20, 2011 at 7:11
1 Answer
Reset to default 4The function(){ test(a); }
inside your onclick is the cause of the error.
You'd need to use (function(){ test(a); })
to get a function expression instead of a function statement.
However, since a
is not global and JavaScript inside an HTML on*
argument won't create a closure the code still doesn't work.
Here's a proper/working example using jQuery:
function test(a) {
if(a !== undefined) {
$('body').html(a);
}
else {
$('body').html('a is undefined');
}
var a = 'a is defined';
$('body').append('<br /><br />');
$('<a href="#">test</a>').click(function(e) {
e.preventDefault();
test(a);
}).appendTo('body');
}
$(document).ready(function() {
test();
});
本文标签: javascriptUncaught SyntaxError Unexpected token (Stack Overflow
版权声明:本文标题:javascript - Uncaught SyntaxError: Unexpected token ( - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744357287a2602367.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论