javascript
  • if-statement
  • alert
  • bookmarklet
  • bookmarks
  • 2011-12-13 70 views 2 likes 
    2

    我正在尝试编写一个小书签,用于检测用户是否在已解析页面上,如果他不是值,则应将其传递给API。Javascript Bookmarklet If-Else

    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;

    我在做什么错?

    回答

    1

    这应该为你工作:

    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语句稍微使其更加坚固。

    +0

    谢谢,工作很棒!我只需要将双引号改为单引号。 – mmackh 2011-12-13 18:08:52

    0

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

    相关问题