How to write this window.history.go(-historyCount) correct

Tags: ,



In a piece of code that I am writing, I need the user to go back in its history depending on the number of products there are in a shopping cart.

So I wrote this code:

var productCount = $('.rdsubs-cart-name').length;
var historyCount = productCount + 1;
console.log(historyCount);
$("a.btn-continue").click(function() {
  window.history.go(-historyCount);
});

But as I expected this part is written wrong. window.history.go(-historyCount);

I don’t know the naming so it is really difficult for me to find something about it in Google. So I was hoping one of you could point me in the right direction!

Thanks!

Some More Info:

When there are 4 products in the cart and the user removes 1 product then it reloads the page. So when the user removes all 4 products it has also reloaded the page 4 times. So if the user clicks on continue shopping then it needs to go back into the history 5 pages. Otherwise, there would be an endless loop of reloading the shopping cart.

When I write window.history.go(-5); it works but when I write window.history.go(-historyCount); nothing happens.

The code was actually correctly written! The culprit was my browser. After opening another browser and going to the same page everything worked as expected. I am thankful for everyone helping out!

Answer

The code was actually correctly written! The culprit was my browser. After opening another browser and going to the same page everything worked as expected. I am thankful for everyone helping out!



Source: stackoverflow