18

Is it possible to get a browser's home page using Javascript?

I'd like to place a link on a page that goes to the home page set in the browser.

Mark Biek
  • 135,050
  • 52
  • 150
  • 195
Trev
  • 1,318
  • 3
  • 16
  • 28

5 Answers5

8

EDIT: simplified answer

Identify browsers and:

  • Call window.home(); for all browsers

  • Call window.location.href = "about:home"; for IE

To do so you can use http://jquery.thewikies.com/browser/

The jQuery Browser Plugin is an addon for jQuery that makes it easy to uniquely identify your visitors' browsers.


Other solutions:

 <script language="javascript">
    function gohome(){
      if (typeof window.home == 'function'){ // The rest of the world
        window.home();
      } else if (document.all) { // For IE 
        window.location.href = "about:home";
      } else {
        document.write("<p>Please click on your browser's Home
button.</p>");
      }
    }
  </script>

This is via this website. The poster states that there are issues to target Safari. This can be fixed using this other website.

Using the CSS tricks explained there you can then do:

<script type="text/javascript">
   isSafari3 = false;
   if(window.devicePixelRatio) isSafari3 = true;
</script>

and use this in the script above to call the correct function:

if (typeof window.home == 'function' || isSafari3)
marcgg
  • 60,067
  • 49
  • 172
  • 221
  • 1
    Looking at it now, this answer doesn't seem to be 100% correct. `about:home` does not seem to work in IE 8, and `window.home()` only works in Firefox (not Safari or Chrome). – PleaseStand Dec 03 '10 at 02:48
  • 1
    In case this helps others: had a confounding problem on a legacy web app I just started maintaining after upgrading from FF 21 to FF 22.0: it had an iframe with `name="home"` and Javascript like `var url = top.home.location.href`, but this broke since apparently `home` is a function in Firefox [that cannot be overwritten like it could in 21.0]. – nothingisnecessary Jul 02 '13 at 19:53
2

Default home page (default new tab) URL:

Google Chrome:

https://www.google.com/_/chrome/newtab

Firefox and IE:

about:home

Opera:

opera:speeddial

Safari:

http://livepage.apple.com

To find out the default home page URL of your browser, go to your home page and type location.href in the console. Note that the browser might redirect you to your locale, so you'll need to find out the page before redirection (it happens on Chrome).


If you're using this browser detection code you can use this one-liner to get the correct url:

var homepageurl = browser == 'gc' ? 'https://www.google.com/_/chrome/newtab' : browser == 'op' ? 'about:speeddial' : browser=='sa' ? 'http://livepage.apple.com' : 'about:home'

Browser detection code JSFiddle: https://jsfiddle.net/oriadam/ncb4n882/

oriadam
  • 5,404
  • 2
  • 35
  • 39
1

Not sure if there is a cross-browser solution. In IE you can use the HomePage behavior and call navigateHomePage.

i_am_jorf
  • 51,120
  • 15
  • 123
  • 214
0

For FF and the like: window.home();

For IE: location = "about:home";

Kyle Rosendo
  • 23,930
  • 7
  • 75
  • 114
0

window.home() didn't work for me in FF37, but this was fine:

location.href = "about:home";
harvest316
  • 1,382
  • 13
  • 21