1

I am working on a website with Wordpress where posts are to be loaded with AJAX while changing the url to the actual permalink of the post. Like how pinterest works. The AJAX loaded content is displayed in a modal box. I use

window.history.pushState(stateObj, title, url) to change the url of the browser to the post's permalink. Now my problem is:

When the modal box closes, I want to go to the previous url without reloading the page.

e.g. I am on the homepage (http://example.com), then i click a post, modal box opens with the post content fetch through AJAX and the url become (http://example.com/post1). When the modal box closes, I would like the url to go back to (http://example.com) without actual page reload. So basically only to the previous state.

nezgerland
  • 361
  • 1
  • 4
  • 12

1 Answers1

2

So use same way of changing url like on open but trigger it on close event of modal window.

EDIT: To go to the root url you can do window.history.pushState('', '', '/')

yakxxx
  • 2,841
  • 2
  • 21
  • 22
  • Tried this way, (on close event): window.history.pushState('', '', ''); since i want to go to http://example.com from http://example.com/post1 But that does not affect the url since it only means there is nothing to concatenate to the url. – nezgerland Oct 14 '12 at 13:37
  • If I want to go to http://example.com/post1 from http://example.com/someotherpage, how can i achieve that? – nezgerland Oct 14 '12 at 14:13
  • window.history.pushState('', '', '/post1') generally from where you are going doesn't matter – yakxxx Oct 14 '12 at 20:42