JavaScript compatible IE,FF settings home, add favorite code

Source: Internet
Author: User

If you want to be compatible with multiple browsers to determine the user's browser type, if it is Firefox or ie6,7,8, so that they can not be judged by the difference between the operation.

<title> Untitled Page </title>
<script language= "Web Effects" type= "Text/javascript" >
function Addfavorite (sURL, Stitle)
{
Try
{
Window.external.addfavorite (sURL, stitle);
}
catch (E)
{
Try
{
Window.sidebar.addpanel (Stitle, sURL, "");
}
catch (E)
{
Alert ("Join collection failed, please use Ctrl+d to add");
}
}
}
function Sethome (OBJ,VRL) {
try{
Obj.style.behavior= ' url (#default #homepage) '; Obj.sethomepage (VRL);
}
catch (e) {
if (Window.netscape) {
try {
Netscape.security.privilegemanager.enableprivilege ("Universalxpconnect");
}
catch (e) {
Alert ("This operation is rejected by the browser!") n Please enter "About:config" in the browser address bar and return to n then set the value of [Signed.applets.codebase_principal_support] to ' true ' and double-click. ");
}
var prefs = components.classes[' @mozilla. Org/preferences-service;1 '].getservice ( Components.interfaces.nsiprefbranch);
Prefs.setcharpref (' Browser.startup.homepage ', VRL);
}
}
}
</script>

<body>
<form id= "Form1" runat= "Server" >
<a onclick= "Addfavorite (window.location,document.title)" style= "Cursor:pointer" > Add Favorites </a>
<a onclick= "Sethome (this,window.location)" style= "Cursor:pointer" > Set as Homepage </a>
</form>
</body>

Simple code

//Add to Favorites
Function Addtofavorite ()
{
    if (document.all) {
        Window.external.addfavorite (Document.url,document.title);
   }else if (window.sidebar) {
       Window.sidebar.addpanel ( Document.title, Document.url, "");
   }
}

//Set as homepage
Function sethomepage () {
 if (document.all) {
    Document.body.style.behavior= ' url (#default #homepage) ';
    document.body.sethomepage (Document.url);
 }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, enter About:config in the Address bar, and then signed.applets.codebase_principal_support the value to true ");
      }
  }
    var prefs = components.classes[' @mozilla. Org/preferences-service;1 '].getservice (components. Interfaces.nsiprefbranch);
    prefs.setcharpref (' Browser.startup.homepage ', document.url);
 }
}

Call method

<a href= "#" onclick= "setHomePage ();" > Homepage </a>
<a href= "Javascript:addtofavorite ();" > Add Favorites </a>

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.