Manipulación de URL de Javascript y navegación hacia atrás

I am updating the url with something like this:

window.history.pushState(null, "Page title", "/?param=" + myParamValue);

This works fine, but when the user hits the back button, the url gets updated but the page does not reload.

I have an ajax routine that updates the content but if possible I don't want to mess with re-implementing back/forward navigation, and I just want the page to reload in case of the user hitting back/forward browser buttons.

Q: Is there a way to force the page to reload the given url on browser back/forward actions from the user?

Bonus Q: also, what if the browser doesn't support window.history.pushState on older browsers? Shall I surround that code in a try/catch block?

preguntado el 30 de junio de 12 a las 18:06

2 Respuestas

if(typeof window.history.pushState === 'function')
    //handle your url rewriting
else
    //support for older browsers

As for your question about back-button support, the link provided in the comment provides very nice implementations for the feature

Respondido el 30 de junio de 12 a las 18:06

You should take a look at this jQuery plugin, it might be useful for you:

http://www.asual.com/jquery/address/

I found it reading this thread:

https://stackoverflow.com/questions/116446/what-is-the-best-back-button-jquery-plugin

Espero que ayude.

About the old browser question, I don't think you have to bother with that because you are using ajax, which requires a modern browser too. The main question is: who will visit your web site? If it's mainly people over 40yo, maybe you should bother...

Just my opinion ;)

Edit: Be careful with IE9, it's not handled in it. thx to nbrooks for the info

contestado el 23 de mayo de 17 a las 13:05

Thanks, about the cross-browser support... LOL - someone could be on IE8/9 or smt, but yeah not a big concern :) - JuanIdol

IE 9 handle this !! Doesn't it?? IE8 is another story... :) And as i said:just my opinion. I don't bother with old browser anymore, I just check with PHP and echo a: "go get a real browser" page. It may be dumb, but it's a choice to make. - Maresh

@Maresh nah it's not supported til IE 10 - Nbrooks

wow... It's a shame, IE9 has a good JS engine, and some other stuff i like, It's going back down in my esteem... - Maresh

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.