本文介绍: 目前常用的有三种办法session传递cookie传递url传递url暴露参数,其余的两个保存服务端浏览器中,不会暴露地址栏里面下面依次介绍

先看效果 

 

 目前常用的有三种办法

session传递cookie传递url传递

url暴露参数,其余的两个保存服务端浏览器中,不会暴露在地址栏里面

使用url

 

下面依次介绍


一.session传递

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    &lt;title&gt;HTML1</title&gt;
</head&gt;
<body&gt;
    <h1&gt;Welcome to HTML1!</h1&gt;
    <form method="post" action="receive.html">
        <label for="name">Name:</label>
        <input type="text" id="name" name="name">
        <input type="submit" value="Submit">
    </form>
    <script>
        // 获取表单元素输入框元素
        const form = document.querySelector('form');
        const input = document.querySelector('#name');
        // 在表单提交时将数据保存sessionStorage中
        form.addEventListener('submit', (event) => {
            event.preventDefault();
            sessionStorage.setItem('name', input.value);
            form.submit();
        });
    </script>
</body>
</html>

receive.html 

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>HTML2</title>
</head>
<body>
    <h1>Welcome to HTML2!</h1>
    <p>Name: <span id="name"></span></p>
    <script>
        // 获取session中的数据
        const name = sessionStorage.getItem('name');
        // 将数据渲染页面document.getElementById('name').textContent = name;
    </script>
</body>
</html>

案例说明: 

  1. 在HTML1中,我们使用form标签将数据提交到HTML2页面,并设置methodpostaction为HTML2的文件路径
  2. 在HTML2中,我们使用JavaScript获取session中的数据,然后将数据渲染页面上。我们可以使用sessionStorage对象存储数据,它可以在不同的页面之间共享数据。在这里,我们存储用户输入的姓名,并在HTML2中获取渲染了这个数据。

 

如果传递失败检查一下以下问题

  1. HTML1文件中的表单action属性设置错误,导致数据无法传递到HTML2页面。在这里需要确保HTML1中表单的action属性设置为HTML2的文件路径,如action="html2.html"
  2. HTML2文件中的JavaScript代码没有正确地从sessionStorage中获取数据需要确保使用正确key来获取sessionStorage中的数据。在这里,我们使用的keyname,所以需要确保获取数据代码中使用的也是这个key,如const name = sessionStorage.getItem('name');
  3. 本地演示时,需要确保HTML1和HTML2两个文件都在同一个服务器运行,否则sessionStorage无法正常工作
  4. 如果使用了浏览器的隐身模式,也可能导致sessionStorage无法正常工作。 如果以上几个原因都不是问题的话,建议检查浏览器开发者工具是否相关错误信息警告信息

 


二.cookie传递

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>HTML1</title>
</head>
<body>
    <h1>Welcome to HTML1!</h1>
    <form method="post" action="receive.html">
        <label for="name">Name:</label>
        <input type="text" id="name" name="name">
        <input type="submit" value="Submit">
    </form>
    <script>
        // 获取表单元素输入框元素
        const form = document.querySelector('form');
        const input = document.querySelector('#name');

        // 在表单提交时将数据保存cookie中
        form.addEventListener('submit', (event) => {
            event.preventDefault();
            document.cookie = `name=${input.value}`;
            form.submit();
        });
    </script>
</body>
</html>

receive.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>HTML2</title>
</head>
<body>
    <h1>Welcome to HTML2!</h1>
    <p>Name: <span id="name"></span></p>
    <script>
        // 获取cookie中的数据
        const cookies = document.cookie.split(';');
        let name = '';
        for (let cookie of cookies) {
            cookie = cookie.trim();
            if (cookie.startsWith('name=')) {
                name = cookie.substring(5);
                break;
            }
        }
        // 将数据渲染到页面document.getElementById('name').textContent = name;
    </script>
</body>
</html>

案例解释

  1. 在HTML1中,我们使用form标签将数据提交到HTML2页面,并设置methodpostaction为HTML2的文件路径
  2. 在HTML1中,我们使用JavaScript监听表单的提交事件,在事件处理程序中将数据保存到cookie中,并且在页面跳转之前提交表单。这样,在HTML2中就可以通过cookie获取到数据并渲染了。
  3. 在HTML2中,我们使用JavaScript获取cookie中的数据,并渲染到页面上。我们可以使用document.cookie访问cookie,它返回个字符串,包含所有cookie的键值对。在这里,我们遍历个字符串,找到名为name的cookie,并获取它的值。然后将这个值渲染到页面上。

 三.url传输

 index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>HTML1</title>
</head>
<body>
    <h1>Welcome to HTML1!</h1>
    <form method="get" action="receive.html">
        <label for="name">Name:</label>
        <input type="text" id="name" name="name">
        <input type="submit" value="Submit">
    </form>
    <script>
        // 获取表单元素输入框元素
        const form = document.querySelector('form');
        const input = document.querySelector('#name');

        // 在表单提交时将数据拼接到URL中
        form.addEventListener('submit', (event) => {
            event.preventDefault();
            const url = new URL(form.action);
            url.searchParams.set('name', input.value);
            window.location.href = url.toString();
        });
    </script>
</body>
</html>

receive.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>HTML2</title>
</head>
<body>
    <h1>Welcome to HTML2!</h1>
    <p>Name: <span id="name"></span></p>
    <script>
        // 获取URL中的参数
        const params = new URLSearchParams(window.location.search);
        const name = params.get('name');
        // 将数据渲染到页面上
        document.getElementById('name').textContent = name;
    </script>
</body>
</html>

 

 

案例解释

  1. 在HTML1中,我们使用form标签将数据提交到HTML2页面,并设置methodgetaction为HTML2的文件路径
  2. 在HTML1中,我们使用JavaScript监听表单的提交事件,在事件处理程序中将数据拼接到URL中,并跳转到这个URL。这样,在HTML2中就可以通过URL获取到数据并渲染了。
  3. 在HTML2中,我们使用JavaScript获取URL中的参数,并渲染到页面上。我们可以使用URLSearchParams来访问URL中的参数,它提供了一些实用的方法,如get()set()等。在这里,我们获取名为name的参数,并将它渲染到页面上。

 

原文地址:https://blog.csdn.net/long_songs/article/details/129833003

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_23768.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注