首页

Javascript

Html
Css
Node.js
Electron
移动开发
小程序
工具类
服务端
浏览器相关
前端收藏
其他
关于
公司注册

js添加收藏夹代码(兼容IE,firefox,其他浏览器弹信息提示)

2012年10月23日 发布 阅读(2919) 作者:Jerman

完全兼容IE和firefox的“添加收藏夹代码,chrome,safari会提示信息”添加收藏失败,请同时按”CTRL+D”添加“

js部分

  1. //添加浏览器判断方法
  2. $.isBrowser = function () {
  3. //检测浏览器
  4. var iUserAgent = navigator.userAgent;
  5. var iAppVersion = parseFloat(navigator.appVersion);
  6. var isOpera = iUserAgent.indexOf("Opera") > -1;
  7. var isKHTML = iUserAgent.indexOf("KHTML") > -1 || iUserAgent.indexOf("Konqueror") > -1 || iUserAgent.indexOf("AppleWebKit") > -1;
  8. if (isKHTML) {
  9. var isChrome = iUserAgent.indexOf("Chrome") > -1;
  10. var isSafari = iUserAgent.indexOf("AppleWebKit") > -1 && !isChrome;
  11. var isKonq = iUserAgent.indexOf("Konqueror") > -1;
  12. }
  13. var isIE = iUserAgent.indexOf("compatible") > -1 && iUserAgent.indexOf("MSIE") > -1 && !isOpera;
  14. var isMoz = iUserAgent.indexOf("Gecko") > -1 && !isKHTML;
  15. var isNS4 = !isOpera && !isMoz && !isKHTML && !isIE && (iUserAgent.indexOf("Mozilla") == 0) && (navigator.appName == "Netscape") && (fAppVersion >= 4.0 && fAppVersion <= 5.0);
  16. //此处为检测平台
  17. var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");
  18. var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh");
  19. var isUnix = (navigator.platform == "X11") && !isWin && !isMac;
  20. if (isOpera) {
  21. return "opera";
  22. } else if (isChrome) {
  23. return "chrome";
  24. } else if (isSafari) {
  25. return "safari";
  26. } else if (isKonq) {
  27. return "konq";
  28. } else if (isIE) {
  29. //此处没用userAgent来检测,主要是考虑IE9浏览器按F12可以切换到IE7,IE8;用userAgent会检测不出来
  30. if (/MSIE 6.0/gi.test(navigator.appVersion)) {
  31. return "IE6";
  32. } else if (document.all && !document.querySelector) {
  33. return "IE7";
  34. } else if (document.all && document.querySelector && !document.addEventListener) {
  35. return "IE8";
  36. } else {
  37. return "IE9+";
  38. }
  39. } else if (isMoz) {
  40. return "mozilla";
  41. } else if (isNS4) {
  42. return "ns4";
  43. }
  44. };
  45. //添加收藏夹方法
  46. $('#bookmarkme').click(function () {
  47. var browser = $.isBrowser();
  48. if (browser == "mozilla") {
  49. // Mozilla Firefox Bookmark
  50. // rel="sidebar"时,单击时Mozilla Firefox就会打开添加收藏
  51. if ($(this).attr("rel") == "sidebar") {
  52. if (window.sidebar && window.sidebar.addPanel) {
  53. window.sidebar.addPanel(document.title, window.location.href, '');
  54. };
  55. }
  56. return true;
  57. } else if (/IE/g.test(browser)) {
  58. // IE Favorite
  59. if (window.external && ('AddFavorite' in window.external)) {
  60. window.external.AddFavorite(location.href, document.title);
  61. }
  62. } else if (browser == "opera" && window.print) {
  63. // Opera Hotlist
  64. this.title = document.title;
  65. return true;
  66. } else if (browser == "chrome" || browser == "safari") {
  67. // webkit - safari/chrome
  68. alert('Press ' + (navigator.userAgent.toLowerCase().indexOf('mac') != -1 ? 'Command/Cmd' : 'CTRL') + ' + D to bookmark this page.');
  69. }
  70. return false;
  71. });

HTML

  1. <a id="bookmarkme" title="前端开发:www.cnf2e.com" href="#" rel="sidebar">添加收藏夹</a>

js添加收藏夹,看似简单,网上也有很多代码,但要兼容各浏览器还是不容易的.

版权声明:本站文章除特别声明外,均采用署名-非商业性使用-禁止演绎 4.0 国际 许可协议,如需转载,请注明出处