Heim  >  Artikel  >  Web-Frontend  >  设为首页与加入收藏的JS代码(多浏览器支持)_javascript技巧

设为首页与加入收藏的JS代码(多浏览器支持)_javascript技巧

WBOY
WBOYOriginal
2016-05-16 17:46:561545Durchsuche

最新的支持IE、firefox、chrome有提示信息的代码:

<script type="text/javascript">
//设为首页 www.jb51.net
function SetHome(obj,url){
  try{
    obj.style.behavior='url(#default#homepage)';
    obj.setHomePage(url);
  }catch(e){
    if(window.netscape){
     try{
       netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
     }catch(e){
       alert("抱歉,此操作被浏览器拒绝!
\n请在浏览器地址栏输入“about:config”并回车然后将[signed.applets.codebase_principal_support]设置为'true'");
     }
    }else{
    alert("抱歉,您所使用的浏览器无法完成此操作。
\n您需要手动将【"+url+"】设置为首页。");
    }
 }
}
 
//收藏本站 www.jb51.net
function AddFavorite(title, url) {
 try {
   window.external.addFavorite(url, title);
 }
catch (e) {
   try {
    window.sidebar.addPanel(title, url, "");
  }
   catch (e) {
     alert("抱歉,您所使用的浏览器无法完成此操作。
\n加入收藏失败,请进入新网站后使用Ctrl+D进行添加");
   }
 }
}
</script>
<a href="javascript:void(0);" onclick="SetHome(this,'http://www.jb51.net');">设为首页</a>
<div class="text text2"><a href="javascript:void(0);" onclick="AddFavorite('脚本之家','http://www.jb51.net')">收 藏 本 站</a>

先分享两个设置首页的js代码(不符合标准不谈兼容性):

两种方法将当前页设为你浏览器的主页,一种是将你正在浏览的网页设为首页;另一种是将指定网址设为首页,至于你用哪一种,看你的需要了,各有各适用的场合。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn