Javascript add to favorites and set as homepage(compatible with IE, Chrome and FireFox)

Lionsure 2020-06-27 Original by the website

In order to let users bookmark our website for easy browsing next time, usually add "set as homepage and add to favorites" at the top of the website's homepage. These two functions are implemented by Javascript, the following is their code:

 

1. Set as homepage javascript(compatible with IE, Chrome and FireFox)

function SetAsHomePage(obj){
       var url = window.location.href;
       try{
              //Set as homepage of the IE browser
              obj.style.behavior = 'url(#default#homepage)';
              obj.SetAsHomePage(url);
       }
              catch(e){
                     //Set as homepage of Chrome or Firefox browser
                     if(window.netscape){
                            try{
                                   netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                            }
                            catch (e){
                                   alert("The browser refuses to set the current website as the homepage! To continue, please enter 'about:config' in the browser address bar and press Enter on your keyboard, then double-click [signed.applets.codebase_principal_support] to set its value to 'true'.");
                            }
                            var pre = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
                            pre.setCharPref('browser.startup.homepage',url);
                     }
              }
       }

Call: <a onclick="SetAsHomePage(this)">Set as homepage</a>

 

 

2. Javascript add to favorites(compatible with IE, Chrome and FireFox)

function addFavorite(){
              var url = window.location.href;
              try{
                     window.external.addFavorite(url, "Lionsure");
              }
              catch (e){
                     try{
                            window.sidebar.addPanel("Lionsure", url, "");
                     }
                     catch (e){
                            alert("Failed to add to favorites, please press Ctrl + D to add!");
                     }
              }
       }

Call: <a onclick="AddToFavorites()">Add to favorites</a>