tags:

views:

1405

answers:

6

In testing document.location.href, I have observed that when the user initiates an action that results in javascript that assigns to document.location.href, the new URL is added to the history.

However, if the call is initiated by javascript that is result of, say, state change of an XMLHTTPRequest, the entry for the current page in the history is over-written. Have I characterized this correctly? Is there a way to get the page change to be reflected in the history in this latter case?

+2  A: 

When the URL is changed the browser automatically adds it to the history, and there is no way to stop this.

On the contrary AJAX application coders usually strive to add some kind of history so that user interactivity pattern is not broken.

For adding to the history using AJAX / XMLHttpRequest application, add to the hash.

Dmitri Farkov
That's not true. When the URL is changed as a result of a callback from XMLHttpRequest, it's *not* added to the history, though I want it to be.
wolffiex
I never said it was. I said if you want it to be added, modify the has to some meaningful value for it to get stored in the browser. Hash changes are registered in the browser history.document.location.hash = "page=1" for example.
Dmitri Farkov
But I want to load a new page! I want the server to tell the browser -- as the result of a XMLHttpRequest -- to go to start over at a new URL. I've implemented bookmarking within the page via the hash already -- this is my escape hatch: like "don't load the app, redirect to login".
wolffiex
+2  A: 

You could change the location without having the browser display a Back button like this:

window.location.replace(new_url);

However, the original address remains in the browser's history and may be accessed using something like CTRL+H

Reference:

Ionuț G. Stan
A: 

Alas, your question can't be answered, AJAX requests have nothing to do with browser history, and if you loaded some dynamic content with them, then the user clicked the browser back button, the previous page is loaded (this which was loaded with an ordinary GET or POST request), which corrupts the sequence you display content in.

Dmitri's answers means that you will maintain your own history for the dynamic content using the fragment part of the url (this after the # symbol), maybe you'll provide your own back and forward buttons, but still you're not protected from the effect of the browser back and forward buttons.

If only they had provided some kind of events to handle user clicks on these buttons with the ability to cancel.

Ashraf Sabry
+7  A: 

I was facing the same problem and found this workaround which worked for me

instead of

function onAjaxCallback(evt){
    location.href=newLocation;
}

i wrapped the location.href call around a setTimeout. Seems to do the trick. My history's behaving fine now. Hope that helps

function onAjaxCallback(evt){
    setTimeout(function(){
     location.href=newLocation;
    },0)
}
royston
btw tested on chrome, ff3.5 and ie8
royston
+1 this solved my problem over at http://stackoverflow.com/questions/1629285/how-to-use-jquery-click-event-to-change-href-value-asyncronously-based-on-a-json
pavsaund
+1  A: 

Read the original question more carefully. The question is not about content loaded by an XHR, but about content loaded by a script loaded by an XHR. I had the same problem and the setTimeout method seems to work well.

A: 

study: window.location.replace() and window.location.assign()

Scott Evernden