跨浏览器书签/添加到收藏夹 JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3024745/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:57:55  来源:igfitidea点击:

Cross-browser bookmark/add to favorites JavaScript

javascriptcross-browserbookmarks

提问by Aakash Chakravarthy

Is there any cross-browser bookmark/add to favorites using JavaScript.

是否有任何跨浏览器书签/使用 JavaScript 添加到收藏夹。

Searched for some list but none is working. Can you please suggest any?

搜索了一些列表,但没有一个有效。你能提出任何建议吗?

采纳答案by Gert Grenander

jQuery Version

jQuery 版本

JavaScript (modified from a script I found on someone's site - I just can't find the site again, so I can't give the person credit):

JavaScript(从我在某人网站上找到的脚本修改而来 - 我只是找不到该网站,所以我不能给这个人信用):

$(document).ready(function() {
  $("#bookmarkme").click(function() {
    if (window.sidebar) { // Mozilla Firefox Bookmark
      window.sidebar.addPanel(location.href,document.title,"");
    } else if(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;
    }
  });
});

HTML:

HTML:

<a id="bookmarkme" href="#" rel="sidebar" title="bookmark this page">Bookmark This Page</a>

IE will show an error if you don't run it off a server (it doesn't allow JavaScript bookmarks via JavaScript when viewing it as a file://....

如果您不在服务器上运行它,IE 将显示错误(当以file://....

If you need a more complete script, you can always buy one from this page(No, I'm not affiliated with that site... LOL).

如果您需要更完整的脚本,您可以随时从该页面购买(不,我不隶属于该站点...大声笑)。

回答by Prashant Patil

function bookmark(title, url)
{
 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();//this.title=document.title;
 }
 else if (document.all)
 {// ie
  window.external.AddFavorite(url, title);
 }
}

I used this & works great in IE, FF, Netscape. Chrome, Opera and safari do not support it!

我用过这个并且在 IE、FF、Netscape 中效果很好。Chrome、Opera 和 safari 不支持!

回答by bwarner

I'm thinking no. Bookmarks/favorites should be under the control of the user, imagine if any site you visited could insert itself into your bookmarks with just some javascript.

我想没有。书签/收藏夹应该在用户的控制之下,想象一下,如果您访问的任何站点都可以使用一些 javascript 将自己插入到您的书签中。

回答by ajm

How about using a drop-in solution like ShareThisor AddThis? They have similar functionality, so it's quite possible they already solved the problem.

使用像ShareThisAddThis这样的嵌入式解决方案怎么样?它们具有相似的功能,因此很可能它们已经解决了问题。

AddThis's code has a huge if/else browser version fork for saving favorites, though, with most branches ending in prompting the user to manually add the favorite themselves, so I am thinking that no such pure JavaScript implementation exists.

AddThis 的代码有一个巨大的 if/else 浏览器版本分支,用于保存收藏夹,不过,大多数分支都以提示用户自己手动添加收藏夹结束,所以我认为不存在这样的纯 JavaScript 实现。

Otherwise, if you only need to support IE and Firefox, you have IE's window.externalAddFavorite( )and Mozilla's window.sidebar.addPanel( ).

否则,如果你只需要支持 IE 和 Firefox,你有 IE 的window.externalAddFavorite()和 Mozilla 的window.sidebar.addPanel()。