簡體   English   中英

如何在普通瀏覽器 javascript 中寫入單獨的 html 文件?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM