Javascript Bookmarklet If Else

Javascript Bookmarklet If Else,javascript,if-statement,alert,bookmarklet,bookmarks,Javascript,If Statement,Alert,Bookmarklet,Bookmarks,我正在尝试编写一个bookmarklet,用于检测用户是否在已解析的页面上,以及是否在未解析的页面上。这些值应传递给API if (window.location.indexOf("thequeue.org") >= 0) { alert("Drag the Bookmarklet in your Brookmarks Bar!"); } else { location.href = 'http://thequeue.org/r?id=' + encodeURICompon

我正在尝试编写一个bookmarklet,用于检测用户是否在已解析的页面上,以及是否在未解析的页面上。这些值应传递给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;
}
一般来说,关于bookmarklet的教程很少,但我发现了这个转换工具:,它提供了以下内容:

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}());
但是bookmarklet完全停止工作。我可以确认这是有效的:
javascript:location.href=http://thequeue.org/r?id='+encodeURIComponent(location.href)+'&title='+document.title


我做错了什么?

您可能想先尝试删除所有空白。我不知道这是否是你的问题,但它至少会让你的书签变得不那么难看。

这应该适合你:

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;}})();

请注意,我稍微修改了if语句,使其更加健壮。

谢谢,效果很好!我只需将双引号改为单引号。