Javascript 使用jQuery在单击时添加书签
单击特定按钮时,是否有办法将当前页面保存为书签(通过jQuery或其他方式)?尝试以下方法:Javascript 使用jQuery在单击时添加书签,javascript,jquery,Javascript,Jquery,单击特定按钮时,是否有办法将当前页面保存为书签(通过jQuery或其他方式)?尝试以下方法: if (window.sidebar) // firefox window.sidebar.addPanel(title, url, ""); else if(window.opera && window.print){ // opera var elem = document.createElement('a'); elem.setAttribute('href
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);
}
$(文档).ready(函数(){
$(“a.jQueryBookmark”)。单击(函数(e){
e、 preventDefault();//这将防止锚标记将用户转到链接
var bookmarkUrl=this.href;
var bookmarkTitle=this.title;
if(window.sidebar){//For Mozilla Firefox书签
window.sidebar.addPanel(bookmarkTitle,bookmarkUrl,“”);
}else if(window.external | | document.all){//For IE收藏夹
window.external.AddFavorite(书签URL、书签标题);
}else if(window.opera){//对于opera浏览器
$(“a.jQueryBookmark”).attr(“href”,书签URL);
$(“a.jQueryBookmark”).attr(“标题”,书签标题);
$(“a.jQueryBookmark”).attr(“rel”,“侧栏”);
}else{//适用于不支持
警报(“您的浏览器不支持此书签操作”);
返回false;
}
});
});
此代码取自
/e:
Chrome不支持此类操作,因为安全级别可能会被破坏。我认为jquery书签插件是您需要的。jBrowserBookmark允许您向允许将页面添加到浏览器boookmark列表的站点添加功能。Internet Explorer、Firefox、Opera和Konqueror浏览器支持此功能。您可以获得此功能,因为Chrome不支持此操作,解决方案可能是首先检查正在使用的浏览器是否为Chrome,如果是,则提醒用户不支持书签功能。然后在其他情况下,提供的脚本可以正常工作 例如:
$("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;
}
});
在你自己的浏览器中?或者去一些社交媒体网络?我怎样才能让它在Chrome上工作?在Chrome中,警报消息甚至不会显示…为了防止在Chrome中抛出错误,您应该使用
else if(window.external&&window.external.AddFavorite)
,因为window.external
是在Chrome中定义的,但不是window.external.AddFavorite
。将旧的/死链接替换为另一个,不确定是否是同一作者,但这是搜索此名称时的第一个链接之一。该插件只是其他答案中方法的封装。
$("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;
}
});