[英]How to write to a seperate html file in plain browser javascript?
我知道您可以写入当前的 HTML 文件,但我想写入另一个 html 文件。 For example: I have 2 files, one is index.html
, and the other is completedsurvey.html
I want to write something to the completedsurvey.html from my index.html file. 有没有办法
要做到这一点,大多数浏览器都支持吗?
所以对于第一个解决方案:假设您希望在成功提交表单时显示“用户名”和“摘要”。 这不是一个精确的解决方案,只是您可以用来获取概念的东西。
索引.html
<form>
<input type='text' id="username">
<input type='text' id="summary">
<button type="button" id="process-form">
</form>
<script>
document.getElementById('process-form').addEventListener('click', (eve)=>{
var uname = document.getElementById('username');
var summ = document.getElementById('summary');
localStorage.setItem('uname', uname);//saving the data in local storage
localStorage.setItem('summ', summ);
//redirect to next page...
});
</script>
调查完成。html
<!doctype>
.
.
<div>
<h1 id="name"> </h1>
<p id='summary'> </p>
</div>
<script>
var uname = localStorage.getItem('uname');//retrieving from local storage
var summ = localStorage.getItem('summ');
document.getElementById('name').innerHTML = uname;//fill the fields with data
.
.
</script>
更多链接: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.