我正在使用 Drupal 建立一个网站。在每个页面的标题上,我想要一个图像(由我自定义设计),它将充当自定义的“添加到收藏夹”按钮。单击图像应将网站的 URL 添加到用户浏览器的收藏夹(书签)中。这应该适用于所有浏览器,IE7+、FF、Opera、Chrome。我无法在网上找到很多关于此的信息。我想 javascript 应该可以完成这项工作,但我在 Javascript 方面没有太多经验 :) 所以我需要你的帮助!
5 回答
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>
我遇到了一些 rel="sidebar" 的问题。当我在链接标签中添加它时,书签将在 FF 上工作,但在其他浏览器中停止工作。所以我通过代码添加 rel="sidebar" 来解决这个问题:
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.');
}
});
if (window.sidebar) { // Mozilla Firefox Bookmark
window.sidebar.addPanel(document.title,location.href,"");
它添加了书签,但在侧边栏中。
感谢@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>
<a href="javascript:window.external.addFavorite('http://tiny.cc/snir','snir-site')">..</a>
在 Internet Explorer 中,'addFavorite':和 '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-安全部队