当前输入框内容在另外页面输入框保存的实现(localStorage)

localStorage

当前页面

<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>


</head>
<body>
<div >
    <input type="text" id="name" />
    <a href="保存缓存跳转到另外页面.html">保存缓存跳转到另外页面.html</a>
    <script type="text/javascript">
        var myname = document.getElementById("name");
        var str = localStorage.getItem("inputText");
        if (str) {
            myname.value = str;
        }
        window.onbeforeunload = function () {
            localStorage.setItem("inputText",myname.value);
        }
    </script>

</div>


</body>
</html>

另外页面上保存

<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>


</head>
<body>
<div >
    <input type="text" id="name" />
    <script type="text/javascript">
        var myname = document.getElementById("name");
        var str = localStorage.getItem("inputText");
        if (str) {
            myname.value = str;
        }
        window.onbeforeunload = function () {
            localStorage.setItem("inputText",myname.value);
        }
    </script>
</div>


</body>
</html>


版权声明:本文为hdz52013原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。