1

I'm trying to redirect to a page using the following window.location to another page however the data inside the browser's localStorage is being deleted. I'm storing the data by using JSON stringify to an array:

function replaceCartStorage(arr){
    if(arr.length==0){
        localStorage.removeItem("cart");
        window.location = "http://localhost/cw2/Basket.html";
    }else{
        localStorage["cart"] = JSON.stringify(arr);
        window.location = "http://localhost/cw2/Basket.html";
    }
}
Dav
  • 179
  • 2
  • 8
  • 17
  • What do you mean? If you redirect to a different domain, the data won't be available there ! – adeneo Apr 19 '13 at 17:36
  • localStorage is stored per domain, so if you're redirecting from `localhost` to `localhost` you should be able to access the data there. If you're saying that the data gets deleted there's probably some problem with your `arr` resulting in its length always returning 0. – m90 Apr 19 '13 at 17:39
  • @m90 thanks for the reply. the arr is working fine. I have tested it before using this window.location and the data inside the storage is being updated. – Dav Apr 19 '13 at 17:55
  • @adeneo thanks for the reply. the domain is localhost/cw2 then every other page is inside folder "cw2" I would like to go one to another saving localstorage staying inside the same domain. – Dav Apr 19 '13 at 18:00
  • hi guys again. the localstorage works per page? like: /index.hmtl has its own localStorage and /basket.html has its own localstorage right? – Dav Apr 19 '13 at 18:02
  • @Dav no it works per domain, so index.html and basket.html should be able to share the localStorage as long as they're served from the same domain – m90 Apr 19 '13 at 18:05
  • @m90 found what was going on. Every page I load I need to access the storage to be able to view it inside browser's resources. Thanks. var cSession = localStorage.getItem("cart") – Dav Apr 20 '13 at 06:56
  • Hey @Dav, can you post your code after the changes? I'm having troubles with storage too, please see: http://stackoverflow.com/questions/27733340/ngstorage-not-working-properly-when-redirect-using-window-location – Eduardo Jan 01 '15 at 19:38

0 Answers0