Home >Web Front-end >JS Tutorial >JavaScript sets homepage and favorites pages

JavaScript sets homepage and favorites pages

高洛峰
高洛峰Original
2016-11-25 11:52:531436browse

Java code

<html> 
   <head> 
      <title>JavaScript设置首页和收藏页面(兼容IE和火狐)</title> 
      <script> 
      //url:要收藏的链接;title:收藏项的名称 
      function AddFavorite(url,title) 
      { 
         //如果url或者title为空,默认为当前页面url和title。 
         if(!(url&&title)) 
         { 
            url=document.URL; 
            title=document.title;  
         } 
 
         if (document.all)//IE 
         { 
               window.external.addFavorite(url,title); 
         } 
         else if (window.sidebar)//火狐 
         { 
            window.sidebar.addPanel(title, url, ""); 
         } 
      } 
 
      //url:要设置为首页的链接 
      function SetHomepage(url) 
      { 
         //如果url为空,默认为当前页面url。 
         if(!url) 
         { 
            url=document.URL; 
         } 
 
         if (document.all)//IE  
         { 
            document.body.style.behavior = &#39;url(#default#homepage)&#39;; 
            document.body.setHomePage(url); 
         } 
         else if (window.sidebar)//火狐 
         { 
            if (window.netscape) 
            { 
               try 
               { 
                  window.netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
               } 
               catch (e) 
               { 
                  alert("此操作被浏览器拒绝!请在浏览器地址栏输入“about:config”并回车然后将[signed.applets.codebase_principal_support]的值设置为&#39;true&#39;,双击即可。"); 
               } 
            } 
            var prefs = Components.classes[&#39;@mozilla.org/preferences-service;1&#39;].getService(Components.interfaces.nsIPrefBranch); 
            prefs.setCharPref(&#39;browser.startup.homepage&#39;, url); 
         } 
      }  
      </script> 
   </head> 
   <body> 
      <a onClick="SetHomepage(&#39;http://www.www.com/&#39;)" title="设为首页" href="http://www.php.cn/">    
         <a onClick="AddFavorite(&#39;http://www.www.com/&#39;,&#39;在线笔记&#39;)" title="收藏" href="http://www.php.cn/">    </body> 
</html>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn