我们介绍一个可兼容所有浏览器的加入收藏代码代码,大概原理是这样的我们根据获取用户navigator.useragent.tolowercase()信息来判断浏览器,根据浏览器是否支持加入收藏js命令,如果可以自动收藏否则就提示ctrl+d手动收藏了。 1 function addfavorite2() { 2 var url = window.location; 3 var title = document.title; 4 var ua = navigator.useragent.tolowercase(); 5 if (ua.indexof(360se) > -1) { 6 alert(由于360浏览器功能限制,请按 ctrl+d 手动收藏!); 7 } 8 else if (ua.indexof(msie 8) > -1) { 9 window.external.addtofavoritesbar(url, title); //ie810 }11 else if (document.all) {12 try{13 window.external.addfavorite(url, title);14 }catch(e){15 alert('您的浏览器不支持,请按 ctrl+d 手动收藏!');16 }17 }18 else if (window.sidebar) {19 window.sidebar.addpanel(title, url, );20 }21 else {22 alert('您的浏览器不支持,请按 ctrl+d 手动收藏!');23 }24 }
复制本页面代码保存成js文件,然后在要收藏的地方加上
<a href=# rel="sidebar" onclick="javascript:addfavorite2()">加入收藏</a>
如果 <a>上面没有加 rel=sidebar ,那么将会报错: window.sidebar.addpanel is not a function设为首页function sethome(obj,vrl)
{ try { obj.style.behavior='url(#default#homepage)';obj.sethomepage(vrl); } catch(e){ if(window.netscape) { try { netscape.security.privilegemanager.enableprivilege(universalxpconnect); } catch (e) { alert(此操作被浏览器拒绝!\n请在浏览器地址栏输入“about:config”并回车\n然后将[signed.applets.codebase_principal_support]设置为'true'); } var prefs = components.classes['@mozilla.org/preferences-service;1'].getservice(components.interfaces.nsiprefbranch); prefs.setcharpref('browser.startup.homepage',vrl); } } }
<a onclick="sethome(this,window.location)" style="cursor:hand">设为首页</a>
本文讲解了javascript 设为首页 加入收藏夹 js代码 ,更多相关内容请关注。
相关推荐:
对js的继承的理解
js解密,在线js解密解密
如何优化js代码
以上就是javascript 设为首页 加入收藏夹 js代码的详细内容。
