Javascript书签脚本的If-Else

3

我正在尝试编写一个书签小程序,用于检测用户是否在已经解析的页面上,如果不是,则需要将值传递给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;
}

一般来说,关于书签脚本的教程比较少,但我发现了这个转换工具: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}());

然而,书签工具完全停止工作了。我可以确认这个有效:javascript:location.href='http://thequeue.org/r?id='+encodeURIComponent(location.href)+'&title='+document.title; 我做错了什么?
2个回答

2
这个应该适用于你:
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 语句的写法,使其更加健壮。

谢谢,非常好用!我只需要将双引号改成单引号。 - mmackh

0

你可能想先尝试去掉所有的空格。我不知道这是否是你的问题,但至少可以让你的书签脚本看起来更加美观。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接