How to pass a variable's value to the next page with JavaScript?

后端 未结 1 1288
忘了有多久
忘了有多久 2021-01-26 18:05

I want to be able to pass D, I, H, and A to the next page. My code on that page needs to be able to access their values. Here

1条回答
  •  孤独总比滥情好
    2021-01-26 18:37

    In test1.html you write

    localStorage.setItem("Dpoint", D); //passing this value to next page
    localStorage.setItem("Ipoint", I); //passing this value to next page
    localStorage.setItem("Hpoint", H); //passing this value to next page
    localStorage.setItem("Apoint", A); //passing this value to next page
    

    and in test2.html you get the values like this

    D = localStorage.getItem("Dpoint"); 
    I = localStorage.getItem("Ipoint");
    H = localStorage.getItem("Hpoint"); 
    A = localStorage.getItem("Apoint"); 
    

    Note: This works only in modern browsers that support HTML5 local storage. To make it more robust and work across all browsers you can use some library like YUI which has a module called cache-offline. If you want to implement it yourself you can check the YUI code here

    From security standpoint localstorage and cookies are the far more secure than URL GET params since these enforce the same domain restriction. If your data is more sensitive then you might want to consider it storing in the server side in some encrypted format.

    From persistence standpoint this method only persists data within a browser. If you want cross browser persistence then again you need to think storing the info in the server.

    0 讨论(0)
提交回复
热议问题