Dima Deplov
Dima Deplov

Reputation: 3718

url change without hash

I want to change url when i open big image in pop up window on a current page with preview images. I don't want use window.location.hash feature because i want to manipulate with new url through PHP next and i found this complex to made it with hash. So, I found that I can use HTML5 feature to make this.

window.history.pushState(“object or string”, “Title”, “/new-url”);

My problem is: I want to remove this new-url from page when i close big image. How can i make this, without using

window.history.back();

?

Thanks.

Upvotes: 1

Views: 241

Answers (1)

TRiG
TRiG

Reputation: 10643

Closing the image is not analogous to pressing the back button in the browser. It is analogous to following another link back to the original page. So there's no need to go back. Just pushState again, back to the original URL.

On the other hand, if the person does click the back button in their browser, you want that to bring them back to the original page too. So you need to listen for the popstate event, and, when it's fired, run a function which will remove the popup image:

window.addEventListener("popstate", function(e) {
    hideimage();
}

Read more about the HTML5 history API.

Upvotes: 1

Related Questions