【问题标题】:How do I add an "Add to Favorites" button or link on my website?如何在我的网站上添加“添加到收藏夹”按钮或链接?
【发布时间】:2012-04-19 11:10:04
【问题描述】:

我正在使用 Drupal 构建一个网站。在每个页面的标题上,我想要一个图像(由我自定义设计),它将充当自定义的“添加到收藏夹”按钮。单击图像应将网站的 URL 添加到用户浏览器的收藏夹(书签)中。这应该适用于所有浏览器,IE7+、FF、Opera、Chrome。 我无法在网上找到很多关于此的信息。我想 javascript 应该可以完成这项工作,但我在 Javascript 方面没有太多经验 :) 所以我需要你的帮助!

【问题讨论】:

标签: javascript jquery html bookmarks


【解决方案1】:

jQuery 版本

$(function() {
  $('#bookmarkme').click(function() {
    if (window.sidebar && window.sidebar.addPanel) { // Mozilla Firefox Bookmark
      window.sidebar.addPanel(document.title, window.location.href, '');
    } else if (window.external && ('AddFavorite' in window.external)) { // IE Favorite
      window.external.AddFavorite(location.href, document.title);
    } else if (window.opera && window.print) { // Opera Hotlist
      this.title = document.title;
      return true;
    } else { // webkit - safari/chrome
      alert('Press ' + (navigator.userAgent.toLowerCase().indexOf('mac') != -1 ? 'Command/Cmd' : 'CTRL') + ' + D to bookmark this page.');
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<a id="bookmarkme" href="#" rel="sidebar" title="bookmark this page">Bookmark This Page</a>

【讨论】:

  • @Whymarrh,添加了一个 webkit (Chrome/Safari) 操作。
  • @webmaniacgr 查看更新。是的。请实际上 +1 然后接受。
  • 我试过了,但它在歌剧上不起作用。只有 IE 弹出书签框,Firefox、Chrome 和 safari(Windows) 都提示。
  • Firefox 的专有名词 window.sidebar.addPanel(..) 已被弃用,而 the function was removed in Firefox 23(见第三个项目符号)
  • 更新:2017 年 11 月 很遗憾,相应的组织仍未对此进行标准化。这仍然是最好/唯一的解决方案。
【解决方案2】:
  if (window.sidebar) { // Mozilla Firefox Bookmark
     window.sidebar.addPanel(document.title,location.href,"");

它添加了书签,但在侧边栏中。

【讨论】:

  • location.href,document.title, - 应该是相反的顺序
【解决方案3】:

此代码是 iambriansreed 答案的更正版本:

<script type="text/javascript">
    $(function() {
        $("#bookmarkme").click(function() {
            // Mozilla Firefox Bookmark
            if ('sidebar' in window && 'addPanel' in window.sidebar) { 
                window.sidebar.addPanel(location.href,document.title,"");
            } else if( /*@cc_on!@*/false) { // IE Favorite
                window.external.AddFavorite(location.href,document.title); 
            } else { // webkit - safari/chrome
                alert('Press ' + (navigator.userAgent.toLowerCase().indexOf('mac') != - 1 ? 'Command/Cmd' : 'CTRL') + ' + D to bookmark this page.');
            }
        });
    });
</script>

【讨论】:

  • 谢谢!这适用于 ie 与接受的答案不同..(在 ie10 上强制 ie9 模式)
  • 更新:如果您有一个带有 id=sidebar 的元素,这将失败,请查看:stackoverflow.com/questions/17747578/…
  • 带有 id=sidebar 元素的 Firefox。检查上面的问题..虽然它是固定的
  • 刚试了脚本,但在IE、Firefox、Chrome、Opera和Safari(Windows)上只弹出书签框都提示。
【解决方案4】:

我在使用 rel="sidebar" 时遇到了一些问题。当我在链接标签中添加它时,书签将在 FF 上工作,但在其他浏览器中停止工作。所以我通过代码添加 rel="sidebar" 来解决这个问题:

jQuery('.bookmarkMeLink').click(function() {
    if (window.sidebar && window.sidebar.addPanel) { 
        // Mozilla Firefox Bookmark
        window.sidebar.addPanel(document.title,window.location.href,'');
    }
    else if(window.sidebar && jQuery.browser.mozilla){
        //for other version of FF add rel="sidebar" to link like this:
        //<a id="bookmarkme" href="#" rel="sidebar" title="bookmark this page">Bookmark This Page</a>
        jQuery(this).attr('rel', 'sidebar');
    }
    else if(window.external && ('AddFavorite' in window.external)) { 
        // IE Favorite
        window.external.AddFavorite(location.href,document.title); 
    } else if(window.opera && window.print) { 
        // Opera Hotlist
        this.title=document.title;
        return true;
    } else { 
        // webkit - safari/chrome
        alert('Press ' + (navigator.userAgent.toLowerCase().indexOf('mac') != - 1 ? 'Command/Cmd' : 'CTRL') + ' + D to bookmark this page.');

    }
});

【讨论】:

    【解决方案5】:

    归功于@Gert Grenander@Alaa.KhRoss Shanon

    尝试下订单:

    一切正常 - 除了 Firefox 书签功能。 出于某种原因,“window.sidebar.addPanel”不是调试器的功能,尽管它工作正常。

    问题在于它从调用&lt;a ..&gt; 标签中获取值:title 作为书签名称,href 作为书签地址。 所以这是我的代码:

    javascript:

    $("#bookmarkme").click(function () {
      var url = 'http://' + location.host; // i'm in a sub-page and bookmarking the home page
      var name = "Snir's Homepage";
    
      if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1){ //chrome
        alert("In order to bookmark go to the homepage and press " 
            + (navigator.userAgent.toLowerCase().indexOf('mac') != -1 ? 
                'Command/Cmd' : 'CTRL') + "+D.")
      } 
      else if (window.sidebar) { // Mozilla Firefox Bookmark
        //important for firefox to add bookmarks - remember to check out the checkbox on the popup
        $(this).attr('rel', 'sidebar');
        //set the appropriate attributes
        $(this).attr('href', url);
        $(this).attr('title', name);
    
        //add bookmark:
        //  window.sidebar.addPanel(name, url, '');
        //  window.sidebar.addPanel(url, name, '');
        window.sidebar.addPanel('', '', '');
      } 
      else if (window.external) { // IE Favorite
            window.external.addFavorite(url, name);
      } 
      return;
    });
    

    html:

      <a id="bookmarkme" href="#" title="bookmark this page">Bookmark This Page</a>
    

    在 Internet Explorer 中,“addFavorite”之间存在差异: &lt;a href="javascript:window.external.addFavorite('http://tiny.cc/snir','snir-site')"&gt;..&lt;/a&gt; 和'AddFavorite':&lt;span onclick="window.external.AddFavorite(location.href, document.title);"&gt;..&lt;/span&gt;

    此处的示例:http://www.yourhtmlsource.com/javascript/addtofavorites.html

    重要的是,在 chrome 中我们无法使用 js 添加书签 (aspnet-i): http://www.codeproject.com/Questions/452899/How-to-add-bookmark-in-Google-Chrome-Opera-and-Saf

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多