2

I'm curious as to how I'd get JavaScript to distinguish between two near identical pages which (as far as I can tell) have the same div's. An example of a page like this would be Google Home Page vs. Google Search Results. Is there anyway I can correctly identify which is which?

Skizit
  • 43,506
  • 91
  • 209
  • 269

2 Answers2

1

In that specific example, window.title will distinguish them. window.title isn't supported by Chrome, but document.title is. It works in Chrome, Firefox, and Opera on both Linux and Windows; Safari on Windows; IE6, IE7, and IE8 on Windows; and probably others as well.

More generally, window.location gives you the URL of the page, which is good for telling what page you're on; more on MDC. It's supported on every major browser I've ever seen, including the list above.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • `window.location` isn't reliable in every browser and Google has a way of changing the URL's so that distinguishing them can be quite difficult. However, `window.title` seems like quite a good solution for the Google example. – Skizit Feb 01 '11 at 12:43
  • Hmm.. `window.title` won't reliably work if the language the user is searching in isn't English. – Skizit Feb 01 '11 at 12:45
  • @Skizit: *"`window.location` isn't reliable in every browser"* Huh? I've never heard of `window.location` being unreliable. It works in every browser I've ever seen. For instance: http://jsbin.com/azogi4 I just checked that in Chrome, Firefox, and Opera for both Windows and Linux; Safari under Windows; and IE6, IE7, and IE8 under Windows. I'd be interested to know more about your thing about `window.location`, do you have a reference I can look at? – T.J. Crowder Feb 01 '11 at 12:51
  • Additionally Chrome doesn't support `window.title`. – Skizit Feb 01 '11 at 12:51
  • @T.J.Crowder If you were to use a webkit based browser and be using Google instant window.location won't work properly. Using the URL isn't really an option with a Google page. If you go from Google home page -> iGoogle -> Google home page for example the URL changes syntax completely so that solution would break. – Skizit Feb 01 '11 at 12:55
  • @T.J.Crowder, ah.. document.title seems to be doing the trick :) – Skizit Feb 01 '11 at 12:57
  • @Skizit: Chrome and Safari are both WebKit-based browsers. You talking about Google instant search? That wasn't listed as part of your requirements. Yes, of course the location is completely different for different pages. You have to parse it and do some work. All of that is a **far** cry from "`window.location` isn't reliable in every browser". – T.J. Crowder Feb 01 '11 at 12:58
1

Since HTML5, you can edit the browser history. For example, you can change the current URL with window.history.pushState():

// pushState(state object, title, URL)
window.history.pushState({foo: "bar"}, "page 2", "bar.html");

This makes the user remain on exactly the same page, but changes the URL. This is happening on the current version of Google's homepage too, so the page is still the same.

You can retrieve the URL with window.location.

Harmen
  • 22,092
  • 4
  • 54
  • 76
  • I'm not looking to do this. I want to identify what the current page is. – Skizit Feb 01 '11 at 12:50
  • 1
    You are always on the same page. Search for `pushState` and `onpopstage` in their code. This is how it´s done by Google. Hm, maybe this should have been a comment, rather than an answer – Harmen Feb 01 '11 at 12:55