我的 JS 代码中的一行有问题:
window.setTimeout('window.location.href = "http://holy-war.net/town/alchemist/?w="' + world + ';',2000);
其中 world 是一个包含文本“4IN”(无引号)的字符串。也许我的串联在某种程度上被破坏了,或者您甚至无法将变量传递到 setTimeout 函数中?是哪个?
最佳答案
是的,您的串联已损坏。它将生成代码
window.location.href = "http://holy-war.net/town/alchemist/?w="4IN;
// ^^^
它准确地显示了错误消息所描述的问题。相反:
修正报价
window.setTimeout('window.location.href = "http://holy-war.net/town/alchemist/?w=' + world + '";', 2000);
使用正确的序列化
window.setTimeout('window.location.href = '+JSON.stringify('http://holy-war.net/town/alchemist/?w=' + world) + ';', 2000);
根本不要生成代码来
eval
,按照您应该的方式使用函数!window.setTimeout(function() { window.location.href = 'http://holy-war.net/town/alchemist/?w=' + world; }, 2000);
关于javascript - 语法错误: identifier starts immediately after numeric literal - setTimeout or concatenation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24837989/