Home>

There are more and more browsers now,This makes web design compatibility requirements higher and higher,Like commonly used as homepage, add favorite code,Although generally simple,But compatibility is very bad,Add to favorites and set as homepage code Almost every website puts a header on the headWhether it will work or not,The demand should be there.

However, due to browser compatibility issues,A lot of the code I used before has no effect.Below is a piece of code that is compatible with each browser,Ca n’t be considered compatible,It can only be said that the prompt can be given in an unsupported browser,code show as below:

<! Doctype html>
<html>
<head>
<title>Add to favorites and set as homepage</title>
<script type="text/javascript">
//Add to favorites
function addfavorite (surl, stitle) {
surl=encodeuri (surl);
try {
window.external.addfavorite (surl, stitle);
}
catch (e) {
try {
window.sidebar.addpanel (stitle, surl, "");
}
catch (e) {
alert ("Add to collection failed,Please use ctrl + d to add,Or manually set in the browser.
");
}
}
}
//Setup as front page
function sethome (url) {
if (document.all) {
document.body.style.behavior="url (#default #homepage)";
document.body.sethomepage (url);
}
else {
alert ("Hello, your browser does not support the function of automatically setting the page as the homepage,Please manually set this page as your homepage in your browser! ");
}
}
</script>
</head>
<body>
<a href="javascript:void (0)">Set as homepage</a>
<a href="javascript:void (0)">Add to Favorites</a>
</body>
</html>

Let me share a piece of jquery code to implement the function of adding favorites

//Setup as front 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 operation was rejected by the browser,If i want to enable this feature,Please enter about:config in the address bar, then set the value of the signed.applets.codebase_principal_support to 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;
  }
}
  • Previous Basic configuration example of dynamic and static separation of Nginx with Apache or Tomcat
  • Next Detailed JavaScript UTC time conversion method