当没有任何内容时,Javascript会给我一个错误

时间:2013-10-17 21:18:45

标签: javascript syntax undefined

这段代码工作正常,现在我总是得到一个“意外的令牌”

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        if (xmlhttp.responseText.search("url=") == 0){
            $("#showMessage").after('<div class="message"><center>Copy your new URL:<input type="text" disabled="disabled" value="'+xmlhttp.responseText.substr(4)+'"</input><br /></center></div>');
        else{   
            $("#showMessage").after('<div class="error">'+xmlhttp.responseText+'</div>');
        }
    }
}

这就是所谓的:

<input type="submit" id="btn-submit" value="Submit" onclick="javascript:createURL()"/>

(我也得到了“createURL未定义”,但我猜这是因为语法错误)

2 个答案:

答案 0 :(得分:2)

您需要在else之前使用右括号。

if (xmlhttp.responseText.search("url=") == 0){
   $("#showMessage").after('<div class="message"><center>Copy your new URL:<input type="text" disabled="disabled" value="'+xmlhttp.responseText.substr(4)+'"</input><br /></center></div>');
} else{   
    $("#showMessage").after('<div class="error">'+xmlhttp.responseText+'</div>');
}

答案 1 :(得分:0)

if ... else声明中存在语法错误

现在是:

if(){
...
//<< There should be a closing bracket for the if statement here
else{
...
}

但它应该是:

if(){
...
}
else{
...
}

或者如果ifelse子句中的代码在一行上,您可以跳过括号:

if() ...;
else ...;