How can I set default homepage in FF and Chrome via javascript?

Solution 1:

What you're asking for is generally considered very annoying page behavior and, therefore, isn't widely supported.

A better UX (User Experience) choice is to give a small set of "how-to" instructions on how the users can make your page their homepage in their respective browsers. Give the user the choice!

Solution 2:

You can't do it in FF because of security. Check out this article. Your user would have to change the signed.applets.codebase_principal_support setting to false. Probably not something that is worth counting on.

Solution 3:

I Have found one script which will work both ie & Mozila. But won't work in opera & chrome.

Write below function inside javascript tag

<script type="text/javascript">
function setHomepage()
{
 if (document.all)
    {
        document.body.style.behavior='url(#default#homepage)';
  document.body.setHomePage('http://www.kerala.in');

    }
    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://www.kerala.in');
 }
}
</script>

then Call this function setHomepage() on click of button.

Solution 4:

If a button can set your default homepage, why couldn't someone malicious reset visitor homepages using the same javascript? This is why such a function does not exist on well behaved browsers.

Solution 5:

I know this is an old thread, but I was forced to investigate this today. I thought I'd post an answer with clear information on the problem.

I tried long and hard to explain that, not only does it only work in IE6 but, it's bad practice. Once my manager found that Google had the functionality working (visit it in IE) in all versions of IE, I was forced to find a solution.

So, while document.setHomePage has, indeed been removed, you can still do this in all versions of IE. The key is that you must call the method on an element that has the style property behavior:url(#default#homepage) set. The following link will work in IE if placed on your page. You will have to find other methods for other browsers. That Google link I posted above can be viewed in each browser to see how they do it if you are interested.

<a
    href="#"
    style="behavior: url(#default#homepage);"
    onclick="this.setHomePage('http://google.com');return false;">
        Make Google your Homepage!
</a>

It looks like IE7+ might require this to happen on a click even though. I couldn't get the code to run in console when I tried.

Here's the MSDN page on the behavior. http://msdn.microsoft.com/en-us/subscriptions/ms531418(v=vs.85).aspx

Now to go hang my head in shame.