2012-05-31

Set a webpage as Home page using javascripts

You can use the below code to set a particular page as the home page in a web browser.

<INPUT TYPE="button" VALUE="Set as homepage" onClick="this.style.behavior='url(#default#homepage)'; this.setHomePage('Page URL beginning with http:// here');">

The problem with the above code is, it will not work in Firefox if you have set the signed.applets.codebase_principal_support to false. In that case, use the following code.

function setHome()
  {
    if (document.all)
    {
     document.body.style.behavior=’url(#default#homepage)’;
     document.body.setHomePage(’
http://url_of_page.com’);

    }
    else if (window.sidebar)
     {
         if(window.netscape)
         {
          try
          {
          netscape.security.PrivilegeManager.enablePrivilege(”UniversalXPConnect”);
          }
       catch(e)
      {
    alert(”this action was aviod by your browser,if you want to enable,please enter  about:config in your address line,and change the value of     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://url_of_page.com’);
}
}

Call the setHome() function on click of the button or link

No comments: