jQuery 如何在javascript中添加到所有浏览器的收藏夹/书签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18457572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add to Favorites/Bookmark for all browser in javascript
提问by user2711066
I have some query in Add to favorites/Bookmarked web page using java script.I just go through in this Linkand get this source code. It's been working perfectly for the last month. Yet now it will not work in any browser. Here is my code :
我在使用 java 脚本添加到收藏夹/书签网页中有一些查询。我只是通过此链接并获取此源代码。上个月它一直在完美地工作。但是现在它不能在任何浏览器中工作。这是我的代码:
function CreateBookmarkLink(){
var title = document.title;
var url = document.location.href;
if(window.sidebar){
/* Mozilla Firefox Bookmark */
window.sidebar.addPanel(title, url, "");
}else if(window.external){
/* IE Favorite */
window.external.AddFavorite(url, title);
}else if(window.opera && window.print) {
/* Opera Hotlist */
alert("Press Control + D to bookmark");
return true;
}else{
/* Other */
alert("Press Control + D to bookmark");
}
<a href="javascript:CreateBookmarkLink();">Add to Favorites/Bookmark</a>
It doesn't work in any browser any more and just displays:
它不再在任何浏览器中工作,只显示:
TypeError: window.sidebar.addPanel is not a function
> window.sidebar.addPanel(title, url, "");
类型错误:window.sidebar.addPanel 不是函数
> window.sidebar.addPanel(title, url, "");
Any ideas how to solve it? I also need to add favorites in chrome browser. Any other idea to create bookmark for my website.
任何想法如何解决它?我还需要在 chrome 浏览器中添加收藏夹。为我的网站创建书签的任何其他想法。
回答by Dimitri Reifschneider
Due to security reasons it is not possible to add a bookmark in Google Chrome using Javascript.
出于安全原因,无法使用 Javascript 在 Google Chrome 中添加书签。
Alternatively, you could output a message for using the shortcut:
或者,您可以输出使用快捷方式的消息:
$('#bookmarkme').click(function(){
alert('Press ' + (navigator.userAgent.toLowerCase().indexOf('mac') != - 1 ? 'Command/Cmd' : 'CTRL') + ' + D to bookmark this page.');
});
Since window.sidebar.addPanel
is deprecated and not a standard implementation (https://developer.mozilla.org/en-US/docs/Web/APIWwindow.sidebar), you may use the API to add bookmarks when creating an add-on (https://developer.mozilla.org/en-US/docs/Code_snippets/Bookmarks).
由于window.sidebar.addPanel
已弃用且不是标准实现 ( https://developer.mozilla.org/en-US/docs/Web/APIWwindow.sidebar),您可以在创建附加组件时使用 API 添加书签 ( https:/ /developer.mozilla.org/en-US/docs/Code_snippets/Bookmarks)。
Nevertheless it should not be a high burden for users to add their favorite website as bookmark inside the browser.
尽管如此,用户在浏览器中添加他们喜欢的网站作为书签应该不是一个很大的负担。