JavaScript-based code to achieve compatible with the browser's homepage and add favorites _javascript Tips

Source: Internet
Author: User


There are more and more browsers, which makes the compatibility of web design more and more demanding, like commonly used to set the home page, to add the collection code, general although concise, but the compatibility is very bad, to join the collection and set the first page code almost every site in the head put on a, there is no effect first, the demand should be there.



However, due to browser compatibility issues, the previous use of many of the code has lost its effect, the following gives a section of the browser can be compatible with the code, and can not be considered compatible, can only say in the unsupported browser can give hints, the code is as follows:


<!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("Failed to add to the collection, please use Ctrl+D to add, or manually set it 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 automatic setting page for the home page function, please manually set this page as the home page in the browser!");
}
}
</script>
</head>
<body>
<a href="javascript:void(0)">Set Homepage</a>
<a href="javascript:void(0)">Add to Favorites</a>
</body>
</html>


The above code is brief and easy to understand, can be compatible with each browser to join the collection and set the homepage, there are questions welcome to put forward, small series will promptly contact with you, thank you!



Here's to share a piece of jquery code implementation add Favorites feature


//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 is rejected by the browser. If you want to enable this feature, please enter about:config in the address bar and then the signed.applets.codebase_principal_support value should be 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 Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.