归功于@Gert Grenander、@Alaa.Kh和Ross Shanon
尝试下订单:
一切正常 - 除了 Firefox 书签功能。
出于某种原因,“window.sidebar.addPanel”不是调试器的功能,尽管它工作正常。
问题在于它从调用<a ..> 标签中获取值: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”之间存在差异:
<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