使用jQuery在单击时添加书签

13
有没有一种方法,在单击特定按钮时通过jQuery或其他方式将当前页面保存为书签?

你是在自己的浏览器中使用吗?还是在某个社交媒体网络上? - bastianwegge
4个回答

12
<script language="javascript" type="text/javascript">
$(document).ready(function(){
  $("a.jQueryBookmark").click(function(e){
    e.preventDefault(); // this will prevent the anchor tag from going the user off to the link
    var bookmarkUrl = this.href;
    var bookmarkTitle = this.title;

    if (window.sidebar) { // For Mozilla Firefox Bookmark
        window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
    } else if( window.external || document.all) { // For IE Favorite
        window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
    } else if(window.opera) { // For Opera Browsers
        $("a.jQueryBookmark").attr("href",bookmarkUrl);
        $("a.jQueryBookmark").attr("title",bookmarkTitle);
        $("a.jQueryBookmark").attr("rel","sidebar");
    } else { // for other browsers which does not support
         alert('Your browser does not support this bookmark action');
         return false;
    }
  });
});
</script>

这段代码取自Developersnippets

/e:

Chrome不支持此类操作,因为可能会破坏安全级别。


1
我该如何让它在Chrome中工作?在Chrome中,警告消息甚至都不显示... - conbask
2
为了避免在Chrome中抛出错误,您应该使用else if(window.external && window.external.AddFavorite),因为window.external在Chrome中已定义,但window.external.AddFavorite未定义。 - Johannes Klauß

9

由于Chrome不支持此操作,解决方案可以是先检查使用的浏览器是否为Chrome,如果是,则向用户发出提示,指出书签功能不受支持。对于其他情况,DevelopersSnippets提供的脚本可以正常工作。

示例:

   $("a.bookmark").click(function(e){
    e.preventDefault(); // this will prevent the anchor tag from going the user off to the link
    var bookmarkUrl = this.href;
    var bookmarkTitle = this.title;
    if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) { 
            alert("This function is not available in Google Chrome. Click the star symbol at the end of the address-bar or hit Ctrl-D (Command+D for Macs) to create a bookmark.");      
    }else if (window.sidebar) { // For Mozilla Firefox Bookmark
        window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
    } else if( window.external || document.all) { // For IE Favorite
        window.external.AddFavorite( bookmarkUrl, bookmarkTitle);          
    } else if(window.opera) { // For Opera Browsers
        $("a.bookmark").attr("href",bookmarkUrl);
        $("a.bookmark").attr("title",bookmarkTitle);
        $("a.bookmark").attr("rel","sidebar");
    } else { // for other browsers which does not support
         alert('Your browser does not support this bookmark action');
         return false;
    }
  });

1

试试这个:

if (window.sidebar) // firefox
    window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
    var elem = document.createElement('a');
    elem.setAttribute('href',url);
    elem.setAttribute('title',title);
    elem.setAttribute('rel','sidebar');
    elem.click();
} 
else if(document.all)// ie
    window.external.AddFavorite(url, title);
}

1

我认为 jQuery 书签插件是你在寻找的。jBrowserBookmark 允许你为网站添加功能,使页面可以添加到浏览器的书签列表中。这个功能被 Internet Explorer、Firefox、Opera 和 Konqueror 浏览器支持。你可以在 这里 获取它。


替换了旧/失效的链接为另一个链接,不确定是否是同一位作者,但在搜索这个名字时它是最先出现的链接之一。该插件只是其他答案中方法的封装。 - brasofilo

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