HTML页面传值

在比较古老的前端开发中,如果是使用jsp编写页面,页面之间传值会非常方便,但是如果是使用了HTML,页面之间的传值会一时之间不知道如何解决。下面记录一下项目中遇到的这个问题,以及验证过的解决方案。

本文使用的方法参考了:https://www.cnblogs.com/zxh1919/p/8182080.html

以及菜鸟教程:https://www.runoob.com/jsref/obj-storage.html

简单使用:

在发送参数的页面使用:

sessionStorage.setItem("userId", "A000001");

// 或者可以放置整个json参数
sessionStorage.setItem("data", JSON.stringify(param));

在接收参数的页面使用:

var userId = sessionStorage.getItem("userId");

// 或者直接给指定的字段赋值
document.getElementById("result").innerHTML = localStorage.getItem("userId");

简单使用之后下面介绍一下Window sessionStorage属性。

Web 存储 API 提供了 sessionStorage (会话存储) 和 localStorage(本地存储)两个存储对象来对网页的数据进行添加、删除、修改、查询操作。

  1. localStorage 用于长久保存整个网站的数据,保存的数据没有过期时间,直到手动去除。
  2. sessionStorage 用于临时保存同一窗口(或标签页)的数据,在关闭窗口或标签页之后将会删除这些数据。

localStorage 和 sessionStorage 属性允许在浏览器中存储 key/value 对的数据。sessionStorage 用于临时保存同一窗口(或标签页)的数据,在关闭窗口或标签页之后将会删除这些数据。如果你想在浏览器窗口关闭后还保留数据,可以使用 localStorage 属性, 该数据对象没有过期时间,今天、下周、明年都能用,除非你手动去删除。

浏览器支持,以及其他用法,请参考菜鸟教程:https://www.runoob.com/jsref/obj-storage.html

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
由于浏览器的安全限制,Vue页面无法直接向嵌套的iframe中的html页面传值。但是可以通过以下方式实现: 1. 使用postMessage 在Vue页面中使用postMessage向iframe发送消息,iframe中的html页面监听message事件,接收消息并进行处理。 Vue页面: ```javascript // 发送消息到iframe const iframeWindow = document.getElementById('iframe').contentWindow; iframeWindow.postMessage({data: 'hello'}, '*'); // 监听iframe返回的消息 window.addEventListener('message', (event) => { if (event.origin !== 'http://iframe-origin.com') return; // 验证消息来源 console.log(event.data); // 处理返回的消息 }); ``` iframe页面: ```javascript // 监听消息 window.addEventListener('message', (event) => { if (event.origin !== 'http://vue-origin.com') return; // 验证消息来源 console.log(event.data); // 处理接收到的消息 }); // 发送消息到Vue页面 const parentWindow = window.parent; parentWindow.postMessage({data: 'world'}, 'http://vue-origin.com'); ``` 2. 使用URL参数 在Vue页面中嵌套iframe时,可以在iframe的src属性中添加参数,iframe中的html页面可以通过URL参数获取传递的值。 Vue页面: ```html <iframe src="http://iframe-origin.com?data=hello"></iframe> ``` iframe页面: ```javascript // 获取URL参数 const urlParams = new URLSearchParams(window.location.search); const data = urlParams.get('data'); console.log(data); // 处理获取到的值 ``` 需要注意的是,使用URL参数传递数据时,需要考虑参数的安全性,避免出现安全漏洞。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北冥牧之

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值