我有一个单词作为自定义“添加到收藏夹”按钮。单击单词链接应将网站的URL添加到用户浏览器的收藏夹(书签)。这适用于所有浏览器,IE7 +,FF,Opera,Chrome。由于我的代码正在运行,但我认为我缺少某些东西,它不会在点击时自动书签而不是单击对话框警报。请建议
这是我的代码:
$(function(){
function AddFavorite(sURL, sTitle)
{
try
{
window.external.addFavorite(sURL, sTitle);
}
catch (e)
{
try
{
window.sidebar.addPanel(sTitle, sURL, "");
}
catch (e)
{
alert("请使用Ctrl+D进行添加");
}
}
}
$('div.link_r').find('a').on('click',function(){
AddFavorite(location.herf,'新蔡');
return false;
});
HTML:
<div class="link_r">
<a href="#">收藏此页面,开奖即时查看</a>
</div>
答案 0 :(得分:2)
$(function() {
function AddFavorite(sURL, sTitle) {
if (/firefox/i.test(navigator.userAgent)) {
return false; //firefox work with attr "rel=sidebar"
} else if (window.external && window.external.addFavorite) {
window.external.addFavorite(sURL, sTitle);
return true;
} else if (window.sidebar && window.sidebar.addPanel) {
window.sidebar.addPanel(sTitle, sURL, "");
return true;
} else {
var touch = (navigator.userAgent.toLowerCase().indexOf('mac') != -1 ? 'Command/Cmd' : 'CTRL');
alert('Press ' + touch + ' + D to bookmark this page.');
return false;
}
}
$("div.link_r a").attr("rel", "sidebar").click(function() {
return !AddFavorite(window.location.href, $(this).attr("title"));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="link_r">
<a href="#" title="新蔡">收藏此页面,开奖即时查看</a>
</div>
请参阅:How do I add an "Add to Favorites" button or link on my website?
答案 1 :(得分:0)
这适用于某些浏览器:
$('#bookmark_me').click(function(e){
e.preventDefault();
var bookmarkURL = this.href;
var bookmarkTitle = this.title;
try {
if (window.sidebar) { // moz
window.sidebar.addPanel(bookmarkTitle, bookmarkURL, "");
} else if (window.external || document.all) { // ie
window.external.AddFavorite(bookmarkURL, bookmarkTitle);
} else if (window.opera) { // duh
$('a#bookmark').attr('href',bookmarkURL);
$('a#bookmark').attr('title',bookmarkTitle);
$('a#bookmark').attr('rel','sidebar');
}
} catch (err) { // catch all incl webkit
alert('Sorry. Your browser does not support this bookmark action. Please bookmark this page manually.');
}
});