且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

LocalStorage-通过表单保存名称-在其他页面上显示

更新时间:2023-12-03 09:11:34

以下是使用 sessionStorage 的建议方法.

Here's a suggested approach by using sessionStorage.

但是,您也可以将 localStorage 与相同的实现方法一起使用.它们之间的唯一区别是,即使您关闭浏览器, localStorage 也会保留数据.在这种情况下,保存 userName 我认为***使用 sessionStorage .

However, you can also use localStorage with the same implementation method. The only differences between them is localStorage will keep the data even if you close your browser. Which in this case, saving userName I think it's better to use sessionStorage.

1.html

1.html

<form action="2.html" onsubmit="callme()">
    <p>Enter your name</p>
    <input id="tbName" type="text">
    <button type="submit" value="submit">Submit</button>
</form>
<script>
    function callme(){
        var name = document.getElementById('tbName').value;
        sessionStorage.setItem('userName', name);
    }
</script>

2.html

2.html

<h1 id="welcome"> </h1>
<script>
    window.onload = function() {
        document.getElementById('welcome').innerText = "Hello, " + sessionStorage.getItem('userName');
    };
</script>