最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if homepage using window.location - Stack Overflow

programmeradmin6浏览0评论

Is it possible to check if I'm on the homepahge/index of a site using window.location?

I'm currently checking url using

window.location.href.indexOf("/category/something")

but how can I check for homepage? It doesn't contain any segments.

Note: I don't know what the homepage URL will be so I can't use for example a url name like window.location.href.indexOf("myhomepage.html")

Update: The only clue that I have is that the homepage has no URL segments.

Is it possible to check if I'm on the homepahge/index of a site using window.location?

I'm currently checking url using

window.location.href.indexOf("/category/something")

but how can I check for homepage? It doesn't contain any segments.

Note: I don't know what the homepage URL will be so I can't use for example a url name like window.location.href.indexOf("myhomepage.html")

Update: The only clue that I have is that the homepage has no URL segments.

Share Improve this question edited Feb 8, 2015 at 14:07 CyberJunkie asked Feb 8, 2015 at 13:50 CyberJunkieCyberJunkie 22.7k61 gold badges154 silver badges219 bronze badges 7
  • By "homepage" do you mean the domain root? – isherwood Commented Feb 8, 2015 at 13:52
  • If you don't know anything about the URL of your homepage then there is no way to test if you are on it by examining the URL. – Quentin Commented Feb 8, 2015 at 13:54
  • The URL doesn't necessarily represents the actual page/file/action the server is serving/invoking. Typically, you'll need to rely on the application code to provide this information (and possibly expose it to the client-side as well). – haim770 Commented Feb 8, 2015 at 13:54
  • possible duplicate of Get host name in JavaScript – isherwood Commented Feb 8, 2015 at 13:55
  • 1 does the page (header/footer maybe) contain a link to the homepage? If so, grab that <a> and compare href to window.location.href – firien Commented Feb 8, 2015 at 13:58
 |  Show 2 more comments

4 Answers 4

Reset to default 7

The term homepage itself is a fairly vague construct and not something that is technically identifiable. You could have several different landing pages depending on your screen size, device, credentials, browser, date/time, geolocation, etc. etc.

The only way you can ensure you are on one of these landing pages is to be in control during the initial GET request to the domain (e.g. http://www.example.com).

So, if you're already on the page, there's no real way to know how you got there and if this is the default page provided from that domain, though there are hacks you could try to get a general (albeit very error-prone) idea.

For example, you could compile a list of common homepage paths:

var homepages = [ '/', 'index.html', 'index.htm', 'index.php', 'main.html', 'main.htm', 'homepage.html', 'index2.htm' ]; 

Then compare to the provided window.location.pathname:

if (homepages.indexOf(window.location.pathname) >= 0) {
    // we might be on a "homepage"
}

Many site's homepage, including stackoverflow contain a link to that same homepage.

// browser url = http://example.com
<a href="http://example.com">my site</a>

If you have access to the source, you can identify this link server-side

<a id="homepage" href="http://example.com"/>my site</a>

So, to check if you are on the homepage:

document.addEventListener('DOMContentLoaded', function(e) {
  var link = document.querySelector('#homepage');
  if (link.href == window.location.href) {
    //i'm on the homepage
  }
})

So i just saw your update to the question. if you know there are no url segments, wouldn't window.location.pathname always be "/"

If you simply did window.location and checked the args in there, if there are no pages/paths appended, url matches origin, etc., would that not be good enough to detect -- Idk, I've never had a need to do this? This might also work for .NET and 'other' types of HTML naming conventions (i.e. index.html vs index.htm). Additionally, if someone changes the doc root, or a home page pointer, you'd more or less know (aka not care) because window.location you can check the following:

window.location
Location {replace: function, assign: function, ancestorOrigins: DOMStringList, origin: "http://stackoverflow.com", hash: ""…}ancestorOrigins: DOMStringListassign: function () { [native code] }hash: ""host: "stackoverflow.com"hostname: "stackoverflow.com"href: "http://stackoverflow.com/"origin: "http://stackoverflow.com"pathname: "/"port: ""protocol: "http:"reload: function reload() { [native code] }replace: function () { [native code] }search: ""toString: function toString() { [native code] }valueOf: function valueOf() { [native code] }__proto__: Location

which has path = '/'. That's pretty indicative of homepage'ish'ness.

A JavaScript knows only it's current context. It has no idea where in a site's hierarchy it is, so no, you can't check if you're on the homepage without already knowing the homepage's URL

发布评论

评论列表(0)

  1. 暂无评论