如何在我的网站上添加“添加到collections夹”button或链接?

我正在使用Drupalbuild立一个网站。 在每个页面的标题,我想有一个单一的图像(我自己devise),这将作为一个自定义的“添加到collections夹”button。 点击图片应该添加网站的URL到用户浏览器的collections夹(书签)。 这应该适用于所有浏览器,IE7 +,FF,Opera,Chrome。 我无法find这个在线的很多信息。 我认为,JavaScript应该做的工作,但我没有太多的JavaScript经验:)所以我需要你的帮助!

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> 

此代码是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> 
  if (window.sidebar) { // Mozilla Firefox Bookmark window.sidebar.addPanel(document.title,location.href,""); 

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

我遇到了rel =“sidebar”的一些问题。 当我添加链接标记书签将工作在FF但停止在其他浏览器工作。 所以我通过添加rel =“sidebar”dynamic修复代码:

 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.'); } }); 

感谢@Gert Grenander , @ Alaa.Kh和Ross Shanon

试图做一些命令:

这一切工作 – 除了Firefox书签function。 出于某种原因,“window.sidebar.addPanel”不是debugging器的函数,虽然它工作正常。

问题在于它将调用的标签名称作为书签名称,而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”有一个不同: <a href="javascript:window.external.addFavorite('http://tiny.cc/snir','snir-site')">..</a>和“AddFavorite”: <span onclick="window.external.AddFavorite(location.href, document.title);">..</span>

例如: 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