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

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

This is my first HTML page:

   

    
    
相关标签:
4条回答
  • 2020-12-10 02:25

    Probably the best way in your case to use GET params like:

    http://mysite//second.html?myparams=value
    

    or if it's important or big data - use POST

    0 讨论(0)
  • 2020-12-10 02:27

    Found a solution for you to parse GET variables:

    // Read a page's GET URL variables and return them as an associative array.
    function getUrlVars()
    {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for(var i = 0; i < hashes.length; i++)
        {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    }
    

    Get URL parameters & values with jQuery

    0 讨论(0)
  • 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];
    }
    
    0 讨论(0)
  • 2020-12-10 02:45

    in HTML5 you can use session to pass object from page to another:

    1- create a sesison

    sessionStorage.setItem('key', 'value');
    

    2- read session:

    sessionStorage.getItem('key')
    

    check this example

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