“Uncaught SyntaxError: Unexpected token (”但没有错误
我正在编写一个简单的用户脚本,它将为 Chromium 浏览器提供像 Windows 中的退格按钮导航控制(特别是针对 Linux 用户)。
这个脚本正在工作,然后我对它做了一些修改(非常简单的东西,评论,选项卡,使其变得漂亮),现在我收到了这个错误:
未捕获的语法错误:意外的标记(
在此行上
document.head.appendChild(script);
该脚本位于此处< /a> - 我正在拔出我的头发试图弄清楚这一点。
该脚本实际上仅适用于 chromium,因为 ff 为您提供了启用此功能的配置选项。 - Chromium 15.0.874.106(开发人员版本 107270)Ubuntu 11.10
编辑 如果有人能告诉我为什么这不起作用,那就太好了
EmbedCodeOnPage("(function() {" + fn.toString() + "})();"); // fails
EmbedCodeOnPage("(" + fn.toString() + ")()"); // works.
I'm authoring a simple userscript that will give the backspace button navigation control like in windows (specifically this is for linux users) for Chromium browser.
This script was working, then I made a few alterations to it (very simple stuff, commenting, tabbing, making it pretty), and now i'm getting this error:
Uncaught SyntaxError: Unexpected token (
on this line
document.head.appendChild(script);
The script is located here - i'm pulling out my hair trying to figure this out.
The script really only applies to chromium as ff gives you a configuration option to enable this functionality..
- Chromium 15.0.874.106 (Developer Build 107270) Ubuntu 11.10
Edit if someone can tell me why this doesn't work that would be great
EmbedCodeOnPage("(function() {" + fn.toString() + "})();"); // fails
EmbedCodeOnPage("(" + fn.toString() + ")()"); // works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信实际的错误在这里:
fn.toString
已经像这样格式化你的函数:所以你最终会得到这样的结果:
这显然不是你想要的。您想要执行内部函数。
I believe the actual error is here:
fn.toString
is already going to format your function like this:So you're going to end up with this:
This is clearly not what you want. You want to execute the inner function.
因为你只是放入一个匿名函数而不是执行它,
更改第 46 行并添加 ()
because you are just dropping in an anonymous function and not executing it
change line 46 and add ()
即使功能很简单,我也遇到了同样的问题。这可能是由于函数定义本身的语法不正确,尤其是在 java 到 javascript 之间切换。
如果我在对象定义中声明函数,例如calculateTax(){//somelogic;}并运行,我会得到“SyntaxError: Unexpected token (” - 这是因为函数声明的格式/语法不正确。该错误具有误导性,但是通过将其更改为calculateTax : function(){//somelogic;}希望可以解决这个问题,谢谢。
I had the same problem, even with simple function. This might be due to incorrect syntax of the function definition itself, especially switching between java to javascript.
if i declare function with in an object definition such as calculateTax(){//some logic;} and run, i get "SyntaxError: Unexpected token (" - this is due to the fact that function declaration is not correct format/syntax. The error is misleading, however by changing it to calculateTax : function(){//some logic;} resolves the issue. Hope this helps. Thanks.