How can I pass a value from one HTML page to another using JavaScript?

后端 未结 4 1031
时光取名叫无心
时光取名叫无心 2020-12-10 01:53

This is my first HTML page:

   

    
    
4条回答
  •  庸人自扰
    2020-12-10 02:28

    If you are not using html5, you have these ways to pass the value from one html to another - QueryString/GET/Cookies.

    HTML5 provides two objects localStorage and sessionStorage to save the client data. Both allow user to store the data on local machine. Provides two methods - getItem('Key') and setItem('Key','Value') or we can just store the data in array of localStorage or sesionStorage;

    // Store
    localStorage.setItem("lastname", "abc");
    // Retrieve
    document.getElementById("result").innerHTML = localStorage.getItem("lastname");
    

    The sessionStorage object is similar to the localStorage object except it stores the data for only one session. The data is deleted when user closes the window.

    to remove any item from session:

    localStorage.removeItem("lastname");
    

    to store as an array:

    for (item in items) {
       localStorage[item] = AnyArray[item];
    }
    

提交回复
热议问题