Based on JavaScript code to achieve compatibility of all browsers as the home page and add favorites

  • 2020-11-20 06:00:48
  • OfStack

There are more and more browser, this makes the web design compatibility of the demand is higher and higher, as a commonly used set to the home page, to join the collection code, 1 although simple, but very bad compatibility, add the collection and set to the home page code almost every website one is put in the head, is there any effect regardless, demand should have.

However, due to the compatibility problem of browsers, many of the codes used before are ineffective. The following is a section of code that is compatible with all browsers, which is not compatible, except that it can be suggested in browsers that are not supported. The code is as follows:


<!doctype html>
<html>
<head>
<title> Add favorites and set to home page </title>
<script type="text/javascript">
// Join the collection 
function AddFavorite(sURL, sTitle){
sURL = encodeURI(sURL); 
try{ 
window.external.addFavorite(sURL, sTitle); 
}
catch(e){ 
try{ 
window.sidebar.addPanel(sTitle, sURL, ""); 
}
catch(e){ 
alert(" Failed to add favorites, please use Ctrl+D add , Or manually set it in the browser .");
} 
}
}
// Set to the home page 
function SetHome(url){
if (document.all){
document.body.style.behavior='url(#default#homepage)';
document.body.setHomePage(url);
}
else{
alert(" How do you do , Your browser does not support the automatic setting of the page as the home page , Please manually set this page as the home page in the browser !");
}
} 
</script>
</head>
<body>
<a href="javascript:void(0)"> Set to the home page </a>
<a href="javascript:void(0)"> Join the collection </a>
</body>
</html> 

The above code is short and easy to understand, can be compatible with each browser to join the collection and set as the home page, questions welcome to propose, this site will contact you in a timely manner, thank you!

Below to share 1 jquery code to add favorites function


// Set to the home page 
function SetHomePage() {
  if (document.all) {
    document.body.style.behavior = 'url(#default#homepage)';
    document.body.setHomePage('http://www.87cool.com');
  }
  else if (window.sidebar) {
    if (window.netscape) {
      try {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
      }
      catch (e) {
        alert(" This action was rejected by the browser. If you want to enable this feature, please enter it in the address bar  about:config, Then put the items  signed.applets.codebase_principal_support  The value true");
      }
    }
    var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
    prefs.setCharPref('browser.startup.homepage', 'http://www.87cool.com');
  }
}
// Add to favorites 
function AddFavorite() {
  var title = document.title;
  var url = location.href;
  if (window.sidebar) {
    window.sidebar.addPanel(title, url, "");
  } else if (document.all) {
    window.external.AddFavorite(url, title);
  } else {
    return true;
  }
}

Related articles: