最简短的加入收藏代码

方法一:

 JS部分:

 1 function addFavorite(url,title){
 2          if(window.sidebar||window.opera)return true;
 3          try{
 4              window.external.AddFavorite(url,title);
 5          }
 6          catch(e){
 7              alert("请按下 Ctrl + D 键将本站加入收藏。");
 8          }
 9          return false;
10  }

HTML部分:

1 <a href="http://www.google.com" title="google" rel="sidebar" onclick="return addFavorite('http://www.google.com','Google');">
2                 加入收藏
3 </a> 

示例说明:

1 <a href="网址" title="网站名称" rel="sidebar" onclick="return addFavorite('网址','网站名称');">加入收藏</a>

兼容性:
    火狐,IE可以一键加入收藏夹,谷歌提示Ctrl+D手动加入收藏

另一个版本的JS,和上面的JS是相同的作用:

 1  var address = function (obj, url, title) {
 2          /*
 3           参考源文:http://www.never-online.net/blog/article.asp?id=108
 4           */
 5          var e = window.event || arguments.callee.caller.arguments[0];
 6 
 7          var B = {
 8              IE : /MSIE/.test(window.navigator.userAgent) && !window.opera
 9              , FF : /Firefox/.test(window.navigator.userAgent)
10              , OP : !!window.opera
11              , Safari :navigator.userAgent.indexOf("Safari")>0
12          };
13 
14          obj.onmousedown = null;
15 
16          if (B.IE) {
17              //IE 浏览器,使用 window.external.AddFavorite 方法添加到收藏夹。
18              obj.attachEvent("onmouseup", function () {
19                  try {
20                      window.external.AddFavorite(url, title);
21                      window.event.returnValue = false;
22                  } catch (exp) {}
23              });
24          }else if (B.FF || obj.nodeName.toLowerCase() == "p") {
25              //Firefox 浏览器,使用 window.sidebar.addPanel 方法。
26              obj.setAttribute("rel", "sidebar"), obj.title = title, obj.href = url;
27          }else if (B.OP) {
28              //Opera 浏览器,可以给链接添加 title 和 rel 属性实现收藏功能(该方式也适合Firefox);其中 title 属性就是网站的名称,rel 设置为 slidebar。
29              var a = document.createElement("a");
30              a.rel = "sidebar", a.title = title, a.href = url;
31              obj.parentNode.insertBefore(a, obj);
32              a.appendChild(obj);
33              a = null;
34         }else if(B.Safari){
35             //Safari Chrome 浏览器目前未提供将页面添加到收藏夹接口。 
36              alert("Ctrl+D手动收藏");
37          }
38      }; 

调用方式:

1 <a onMouseDown="address(this, 'http://www.never-online.net/', 'BlueDestiny前辈的BLOG')">添加</a>

示例说明:

 1 <a onMouseDown="address(this, '网址', '网站名称')">添加火狐</a> 

原文地址:https://www.cnblogs.com/BaishangBloom/p/4642756.html