[英]Javascript Bookmarklet If-Else
I'm trying to write a bookmarklet that detects if the user is on an already parsed page and if he isn't values should be passed along to an API. 我正在尝试编写一个书签,该书签可检测用户是否在已解析的页面上,如果不是,则应将值传递给API。
if (window.location.indexOf("thequeue.org") >= 0) {
alert("Drag the Bookmarklet in your Brookmarks Bar!");
} else {
location.href = 'http://thequeue.org/r?id=' + encodeURIComponent(location.href) + '&title=' + document.title;
}
There are few tutorials on Bookmarklets in general, but I have found this conversion tool: http://jasonmillerdesign.com/Free_Stuff/Instant_Bookmarklet_Converter , which gave me this: 一般而言,关于Bookmarklet的教程很少,但是我发现了这种转换工具: http : //jasonmillerdesign.com/Free_Stuff/Instant_Bookmarklet_Converter ,它给了我这个:
javascript:(function(){if(window.location.indexOf(%22queue.org%22)%20%3E%3D%200)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20alert(%22your%20url%20contains%20the%20name%20franky%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%20else%20%7B%0Alocation.href%3D'http%3A%2F%2Fthequeue.org%2Fr%3Fid%3D'%2BencodeURIComponent(location.href)%2B'%26title%3D'%2Bdocument.title%3B%0A%7D}());
However the bookmarklet stops working completely. 但是,小书签完全停止工作。 I can confirm that this works:
javascript:location.href='http://thequeue.org/r?id='+encodeURIComponent(location.href)+'&title='+document.title;
我可以确认这有效:
javascript:location.href='http://thequeue.org/r?id='+encodeURIComponent(location.href)+'&title='+document.title;
What am I doing wrong? 我究竟做错了什么?
This should work for you: 这应该为您工作:
javascript:(function(){if(location.href.indexOf("thequeue.org/")>=0){alert("Drag the Bookmarklet in your Brookmarks Bar!");}else{location.href='http://thequeue.org/r?id='+encodeURIComponent(location.href)+'&title='+document.title;}})();
Notice that i changed the if sentence slightly to make it more robust. 请注意,我稍微修改了if语句以使其更可靠。
You might want to try stripping out all the whitespace first. 您可能想先剥离所有空白。 I don't know if that's your problem, but it will at least make your bookmarklet a lot less ugly.
我不知道这是否是您的问题,但这至少会使您的小书签不那么难看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.