2

I'm wondering is there a way for force window.location to update? I'm on a page which uses ajax to generate results. However, the URL stays the same in the bar and so I can't grab the real URL. I'm wondering.. how would I grab the URL? can I force window.location to update without refreshing the page?

An example of the sort of page I'm talking about is google instant!

Skizit
  • 43,506
  • 91
  • 209
  • 269
  • I think you have to describe the scenario better. What does *I'm on a page which uses ajax* mean? Do you create this page? Are you accessing this page with your browser? How do you access `window.location`? Are you injecting JavaScript in a third party page? etc. – Felix Kling Jan 31 '11 at 11:41
  • Based on your next question, I assume you are creating a Chrome extension that should work on Google Instant pages... am I correct? If so then adding a listener to `hashchange` event is probably the only way to get the URL (but if the accepted answer works, I would be interested in *how* :)) – Felix Kling Jan 31 '11 at 11:57
  • Yes, I'm attempting to create a Chrome extension which works with Google Chrome. However, I can't seem to get the URL of the ajax response. I already have a listener which checks the MD5 for the results to see if the URL has changed but I can't seem to grab the URL. – Skizit Jan 31 '11 at 12:32
  • @Skizit: So I assume you create a content script (or whatever it is called in Chrome), that gets injected into the page? Have you tried registering an `hashchange` event handler? As I said, after a short time (when the user is not typing anymore) the URL gets changed. You would be notified and could get the URL then (if I understood the whole scenario correctly). – Felix Kling Jan 31 '11 at 12:43
  • @Felix Kling Yes, I'm using a content script. if I do that `window.location` just returns `http://www.google.com` not the "real" URL. The problem is the URL isn't changing in `window.location`. However, the url does update in the XHR which chrome console shows. So I'm wondering is there any way to grab that URL and place it in my code. Then use the HTML5 listed below – Skizit Jan 31 '11 at 12:48
  • @Skizit: But why do you want to change the URL? What is the ultimate goal of your extension? As I said, have to you tried using the `hashchange` event handler? – Felix Kling Jan 31 '11 at 13:00
  • I tried the hashchange, I need to record the query. That's why I want the URL. – Skizit Jan 31 '11 at 13:15
  • @Skizit: So there is actually no need to put the URL into `window.location` as long as you get it somehow. `hashchange` should work then but without seeing your script, it is impossible to help. – Felix Kling Jan 31 '11 at 14:30

4 Answers4

1

No you can't. If you don't control the page and they are not updating the URL in a way, then there is nothing you can do.

But if you have control, then it is your responsibility to either use the fragment identifier or HTML5's new history API to update the adress. But even if you use the history API, window.location won't get changed.

Update:

Not 100% sure what Google Instant page you are talking about, but if it is the "normal" Google page, then you will see that the URL updates after 1 or 2 seconds after you typed the query.

So you could add an event listener for the hashchange event and then grab the URL from window.location.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
1

Are you looking to update the content and URL with AJAX? Just use History API provided by HTML5 browsers.

timdream
  • 5,914
  • 5
  • 21
  • 24
  • For browsers not yet support HTML5, you can update URL hash instead. – timdream Jan 31 '11 at 11:36
  • It depends on what the OP *really* wants but using the history API does not change `window.location`. – Felix Kling Jan 31 '11 at 11:39
  • In short: `window.history.pushState({},'the new title','/new-url');` – timdream Jan 31 '11 at 11:39
  • ah, cool, cool. Any suggestions as to how I'd grab the "real" URL from a Google instant page? Having major issues with that! – Skizit Jan 31 '11 at 11:48
  • Google instant does put queries after the hash; `window.location`, `window.location.href`, and `window.location.hash` does contain the information you are looking for. – timdream Jan 31 '11 at 16:32
0

This question will be of interest:

How does facebook rewrite the source URL of a page in the browser address bar?

Community
  • 1
  • 1
spender
  • 117,338
  • 33
  • 229
  • 351
  • It is, but how would I "push" the URL on a Google instant result page into the address bar? – Skizit Jan 31 '11 at 11:35
0

changing location.hash with jquery ui tabs

Add this to your current trigger. (I'm aware this is jQuery but the logic is the same)

Community
  • 1
  • 1
benhowdle89
  • 36,900
  • 69
  • 202
  • 331