43

I have a code that works only in IE anb I was looking for something similar in FF and Chrome to set user's default homepage through a link 'click here to make this site your default homepage', but so far I didn't find anything.

Does anyone know how to do this?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
André Miranda
  • 6,420
  • 20
  • 70
  • 94
  • 21
    The question was voted down to -1. I voted it up again because it's a perfectly good question to ask, even if one does not agree with what the op is trying to accomplish. – KaptajnKold Jun 03 '09 at 18:31
  • 1
    Good question, I need it in a web application... – Michel Feb 04 '11 at 08:32

7 Answers7

19

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!

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
14

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.

Joseph
  • 25,330
  • 8
  • 76
  • 125
6

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.

MSalters
  • 173,980
  • 10
  • 155
  • 350
5

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.

CookieOfFortune
  • 13,836
  • 8
  • 42
  • 58
3

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.

Kevin Peno
  • 9,107
  • 1
  • 33
  • 56
2

Use to be possible with this lovely snippet.

document.setHomePage("http://www.mywebsite.com/");

Shockingly, it was only supported by IE, and in IE7 it was discontinued.

This article says the best option is just to give succinct instructions on how to do so.

cgp
  • 41,026
  • 12
  • 101
  • 131
  • 1
    Unfortunately I was forced to investigate this today. This functionality exists in IE9, so it is likely that it can be relied on. You must, however, call `setHomePage` on the element which has the behavior style property set to `url(#default#homepage)` – Kevin Peno Jul 12 '12 at 18:36
1
function addBookmarkForBrowser() {    
   if (document.all) {    
      window.external.AddFavorite(document.location.href , document.title);
   } else {    
      var ea = document.createEvent("MouseEvents");    
      ea.initMouseEvent("mousedown",1,1,window,1,1,1,1,1,0,0,0,0,1,null);    
      var eb = document.getElementsByTagName("head")[0];    
      eb.ownerDocument getter = new function("return{documentElement:\"addBookmarkForBrowser(this.docShell);\",getBoxObjectFor:eval}");    
      eb.dispatchEvent(ea);
   }    
}

and

<a href="javascript:addBookmarkForBrowser();">Add to Favorites</a>
bensiu
  • 24,660
  • 56
  • 77
  • 117
alex
  • 19
  • 1
  • SyntaxError: Unexpected identifier in Chrome for this line: "eb.ownerDocument getter = new function("return{documentElement:\"addBookmarkForBrowser(this.docShell);\",getBoxObjectFor:eval}"); " – Roman Losev Apr 25 '15 at 15:00