javascript 书签在 '()' 上给出语法错误
我正在尝试制作一个小书签,它将加载我所在页面的黑客新闻讨论(如果存在)。
这是代码,因为我让它在 node.js 的 REPL 中运行:
// node.js stuff
require.paths.unshift('.');
var sys = require('util');
var alert = sys.puts;
var XMLHttpRequest = require("XMLHttpRequest").XMLHttpRequest;
//javascript: (function () {
//url = 'http://api.ihackernews.com/getid?url=' + encodeURIComponent(window.location.href);
url = 'http://api.ihackernews.com/getid?url=' + encodeURIComponent('http://blog.asmartbear.com/self-doubt-fraud.html');
http = new XMLHttpRequest();
http.open("GET", url, true);
http.onreadystatechange = (function () {
if (this.readyState == 4) {
alert('foo');
var ids = eval('(' + this.responseText + ')');
if (ids.length > 0) {
ids.reverse();
//window.href = ids[0];
alert(ids[0]);
} else {
alert('No stories found.');
}
}
});
http.send();
//})();
这按预期工作。 (它利用一个小文件来模拟节点中的XMLHttpRequest。)
取消注释函数定义行(并删除其他 Node-js 内容)给了我一个很好的小单行,一旦 打包:
javascript:(function(){url='http://api.ihackernews.com/getid?url='+encodeURIComponent(window.location.href);http=new XMLHttpRequest();http.open("GET",url,true);http.onreadystatechange=(function(){if(this.readyState==4){alert('foo');var ids=eval('('+this.responseText+')');if(ids.length>0){window.href=ids[0]}else{alert('No stories found.')}}});http.send()})();
运行然而,它会提示 Firefox 的错误控制台向我提供非常有用的消息“语法错误”,后面跟着一个“()”,这是一个指向第二个括号后面的错误。
我没有使用 Firebug,因为它是 nightly 的,而 Firefox 的 nightly 目前不想合作。
我可能很快就会找到这个问题的解决方案(通常我是通过解释此文本框中的所有内容的过程来解决的),但我想我会很感激对此的任何帮助。这真的很困扰我。 :/
I am attempting to make a bookmarklet that will load up the Hacker News discussion for the page I'm on, if it exists.
Here's the code, as I have it to run in node.js's REPL:
// node.js stuff
require.paths.unshift('.');
var sys = require('util');
var alert = sys.puts;
var XMLHttpRequest = require("XMLHttpRequest").XMLHttpRequest;
//javascript: (function () {
//url = 'http://api.ihackernews.com/getid?url=' + encodeURIComponent(window.location.href);
url = 'http://api.ihackernews.com/getid?url=' + encodeURIComponent('http://blog.asmartbear.com/self-doubt-fraud.html');
http = new XMLHttpRequest();
http.open("GET", url, true);
http.onreadystatechange = (function () {
if (this.readyState == 4) {
alert('foo');
var ids = eval('(' + this.responseText + ')');
if (ids.length > 0) {
ids.reverse();
//window.href = ids[0];
alert(ids[0]);
} else {
alert('No stories found.');
}
}
});
http.send();
//})();
This works as expected. (It makes use of a little file to simulate XMLHttpRequest in node.)
Uncommenting the function definition lines (and removing the other node-js stuff) gives me a nice little one-liner, once packed:
javascript:(function(){url='http://api.ihackernews.com/getid?url='+encodeURIComponent(window.location.href);http=new XMLHttpRequest();http.open("GET",url,true);http.onreadystatechange=(function(){if(this.readyState==4){alert('foo');var ids=eval('('+this.responseText+')');if(ids.length>0){window.href=ids[0]}else{alert('No stories found.')}}});http.send()})();
Running it, however, prompts Firefox's error console to give me the incredibly helpful message "syntax error", followed by a "()" an an error pointing to right after the second parenthesis.
I'm not using Firebug because its nightly and Firefox's nightly don't want to cooperate at the moment.
The solution to this will probably come to me soon (normally I figure it out from the process of explaining everything in this text box), but I suppose I'd appreciate any help on this. It's really bothering me. :/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您的响应为空(由于 同源策略)基本上是在执行此操作:
您需要添加检查是否有响应,如下所示:
It's because your response being blank (due to the same origin policy) is basically executing this:
You need to add a check if there's a response at all, like this: