设为首页,及添加收藏

设为首页,以及添加收藏夹代码,随处可见,找到网站,查看源代码即可,一般都是js实现..

代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    
<title>9421 test</title>
</head>
<body>

<script type="text/javascript">
   
function setHomepage(){//设置首页 
   if(document.all){
      document.body.style.behavior
='url(#default#homepage)';
      document.body.setHomePage(
'http://www.9421.org');
   }
   
   
else if(window.sidebar){
        
if(window.netscape){
             
try{
                 netscape.security.PrivilegeManager.enablePrivilege(
"UniversalXPConnect");
             }
             
catch(e){
                 alert(
"您的浏览器未启用[设为首页]功能,开启方法:先在地址栏内输入about:config,然后将项 signed.applets.codebase_principal_support 值该为true即可");
             }
        }
        
var prefs=Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
        prefs.setCharPref(
'browser.startup.homepage','http://www.9421.org');
        alert(
"设置完成!");
   } 
}

function addCookie(){//加入收藏夹 
if(document.all){
       window.external.addFavorite(
'http://www.56beijing.org''9421Test');
}
else if(window.sidebar){
      window.sidebar.addPanel(
'9421Test','http://www.9421.org',"");
}
}
  
</script> 
 
<A  onclick="javascript:setHomepage();" href="#">设置首页 | </a> 
<A  href="#" onclick="javascript:addCookie();"> 加入收藏夹 </a>
   
</body>
</html>

关键代码:

 document.body.setHomePage('http://www.9421.org');

/// var prefs=Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
///prefs.setCharPref('browser.startup.homepage','http://www.9421.org');

 window.external.addFavorite('http://www.56beijing.org', '9421Test');

/// window.sidebar.addPanel('9421Test','http://www.9421.org',"");

 判断浏览器:

if(document.all){
       window.external.addFavorite('http://www.56beijing.org', '9421Test');
}
else if(window.sidebar){
      window.sidebar.addPanel('9421Test','http://www.9421.org',"");

posted @ 2010-01-04 13:34  9421  阅读(510)  评论(2编辑  收藏  举报