Javascript 我如何添加一个“;“添加到收藏夹”;我网站上的按钮或链接?

Javascript 我如何添加一个“;“添加到收藏夹”;我网站上的按钮或链接?,javascript,jquery,html,bookmarks,Javascript,Jquery,Html,Bookmarks,我正在用Drupal建立一个网站。在每个页面的标题上,我希望有一个单独的图像(由我自定义设计),它将充当自定义的“添加到收藏夹”按钮。单击图像应将网站的URL添加到用户浏览器的收藏夹(书签)中。这应该适用于所有浏览器,IE7+、FF、Opera和Chrome。 我在网上找不到这方面的很多信息。我想javascript应该可以完成这项工作,但我在javascript方面没有太多经验:),所以我需要你的帮助 jQuery版本 $(函数(){ $('#bookmarkme')。单击(函数(){ if

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

jQuery版本

$(函数(){
$('#bookmarkme')。单击(函数(){
if(window.sidebar&&window.sidebar.addPanel){//Mozilla Firefox书签
window.sidebar.addPanel(document.title,window.location.href.);
}else if(window.external&('AddFavorite'在window.external中)){//IE Favorite
window.external.AddFavorite(location.href,document.title);
}else if(window.opera&&window.print){//opera热列表
this.title=document.title;
返回true;
}else{//webkit-safari/chrome
警报('按'+(navigator.userAgent.toLowerCase().indexOf('mac')!=-1?'Command/Cmd':'CTRL')+'+D将此页面添加为书签');
}
});
});


它在侧栏中添加书签。

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

<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>

$(函数(){
$(“#书签我”)。单击(函数(){
//Mozilla Firefox书签
如果窗口中的('sidebar')和窗口中的'addPanel'
window.sidebar.addPanel(location.href,document.title,“”);
}如果(/*@cc_on!@*/false){//IE收藏夹
window.external.AddFavorite(location.href,document.title);
}else{//webkit-safari/chrome
警报('按'+(navigator.userAgent.toLowerCase().indexOf('mac')!=-1?'Command/Cmd':'CTRL')+'+D将此页面添加为书签');
}
});
});

我在使用rel=“sidebar”时遇到了一些问题。当我将其添加到链接标签书签将在FF上工作,但在其他浏览器中停止工作。因此,我通过添加rel=“sidebar”dynamic by code来解决这个问题:

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

    }
});
jQuery('.bookmarkMeLink')。单击(函数(){
if(window.sidebar&&window.sidebar.addPanel){
//Mozilla Firefox书签
window.sidebar.addPanel(document.title,window.location.href.);
}
else if(window.sidebar&&jQuery.browser.mozilla){
//对于FF的其他版本,添加rel=“sidebar”如下链接:
//
jQuery(this.attr('rel','sidebar');
}
如果(window.external&('AddFavorite'在window.external中)){
//最爱
window.external.AddFavorite(location.href,document.title);
}如果(window.opera&&window.print){
//歌剧热门榜
this.title=document.title;
返回true;
}否则{
//webkit-safari/chrome
警报('按'+(navigator.userAgent.toLowerCase().indexOf('mac')!=-1?'Command/Cmd':'CTRL')+'+D将此页面添加为书签');
}
});
归功于、和

尝试下一些命令:

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

问题是它从调用


在internet explorer中,“addFavorite”之间有一个不同的名称:
和“AddFavorite”:

示例如下:

重要的是,在chrome中,我们不能使用js()添加书签:

@Whymarrh,添加了一个webkit(Chrome/Safari)操作。@webmaniacgr查看更新。对请实际+1,然后接受。我试过了,但在opera上不起作用。只有IE弹出书签框,Firefox、Chrome和safari(Windows)都发出警报。Firefox的property
window.sidebar.addPanel(…)
已被弃用,并且(见第三项)更新:2017年11月,很遗憾,相关组织尚未对此进行标准化。这仍然是最佳/唯一的解决方案。location.href,document.title,-的顺序应该相反谢谢!与公认的答案不同,这在ie上起作用。。(在ie10上的强制ie9模式下)更新:如果你有一个id=sidebar的元素,这将失败,检查这个:Firefox有一个id=sidebar的元素。检查上面的问题。。它的固定想法只是尝试了这个脚本,但它只在IE、Firefox、Chrome、Opera和Safari(Windows)上弹出书签框,所有警报都是。TL可能重复;DR您可以尝试一个,也可以简单地在页面上创建一个带有`rel=“bookmark”的绝对链接,以便用户可以决定如何使用它。
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.');

    }
});
$("#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;
});
  <a id="bookmarkme" href="#" title="bookmark this page">Bookmark This Page</a>