Cross-browser bookmark/add to favorites JavaScript

Is there any cross-browser bookmark/add to favorites using JavaScript.

Searched for some list but none is working. Can you please suggest any?

196228 次浏览

JQuery 版本

JavaScript (修改自我在某人网站上找到的一个脚本——我就是找不到这个网站,所以我不能给这个人署名) :

$(document).ready(function() {
$("#bookmarkme").click(function() {
if (window.sidebar) { // Mozilla Firefox Bookmark
window.sidebar.addPanel(location.href,document.title,"");
} else if(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;
}
});
});

HTML:

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

如果你没有在服务器上运行 IE 浏览器将会显示一个错误(当你把 IE 浏览器看作 file://...浏览器时,它不允许通过 JavaScript 浏览书签)。

How about using a drop-in solution like 分享这个 or 加上这个? They have similar functionality, so it's quite possible they already solved the problem.

AddThis 的代码有一个巨大的 if/else 浏览器版本分支用于保存收藏夹,不过,大多数分支的结尾都是提示用户自己手动添加收藏夹,因此我认为不存在这样的纯 JavaScript 实现。

Otherwise, if you only need to support IE and Firefox, you have IE's window.externalAddFavorite( ) and Mozilla's window.sidebar.addPanel( ).

function bookmark(title, url) {
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(); //this.title=document.title;
}
else if (document.all)
{
// ie
window.external.AddFavorite(url, title);
}
}

我在 IE,FF,Netscape 中使用这个并且工作得很好。 Chrome, Opera and safari do not support it!