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
 |  Show 2 more ments

1 Answer 1

Reset to default 4

The 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