Sending user to their browser's Home Page usin

2019-01-06 21:56发布

问题:

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.

回答1:

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)


回答2:

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



回答3:

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/



回答4:

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

For IE: location = "about:home";



回答5:

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

location.href = "about:home";