1

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?

JohnIdol
  • 48,899
  • 61
  • 158
  • 242

2 Answers2

3
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

nbrooks
  • 18,126
  • 5
  • 54
  • 66
1

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

I hope it helps.

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

Community
  • 1
  • 1
Maresh
  • 4,644
  • 25
  • 30
  • Thanks, about the cross-browser support... LOL - someone could be on IE8/9 or smt, but yeah not a big concern :) – JohnIdol Jun 30 '12 at 17:21
  • 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 Jun 30 '12 at 17:25
  • 1
    @Maresh nah it's not supported til IE 10 – nbrooks Jun 30 '12 at 17:25
  • 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 Jun 30 '12 at 17:27