CyberJunkie
CyberJunkie

Reputation: 22674

Check if homepage using window.location

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.

Upvotes: 5

Views: 10427

Answers (4)

Nick Jennings
Nick Jennings

Reputation: 4044

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"
}

Upvotes: 7

firien
firien

Reputation: 1568

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 "/"

Upvotes: 4

J Carroll
J Carroll

Reputation: 1391

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.

Upvotes: 2

Suppen
Suppen

Reputation: 856

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

Upvotes: 1

Related Questions