JavaScript設置首頁和收藏頁面

Java代碼 
<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 = 'url(#default#homepage)'; 
            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]的值設置為'true',雙擊即可。"); 
               } 
            } 
            var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch); 
            prefs.setCharPref('browser.startup.homepage', url); 
         } 
      }  
      </script> 
   </head> 
   <body> 
      <a onClick="SetHomepage('https://www.www.com/')" title="設為首頁" href="javascript:">設為首頁</a> 
      <a onClick="AddFavorite('https://www.www.com/','在線筆記')" title="收藏" href="javascript:">收藏</a> 
   </body> 
</html> 

You May Also Like