简体   繁体   中英

How to write to a seperate html file in plain browser javascript?

I know that you can write to the current HTML document, but I want to write to another html file. 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. Is there a way
to do this that is supported in most browsers?

So for the first solution: Assuming you'd want to display "username" and "summary" on successful submission of the form. This isn't an exact solution, just something you can use to get the concept.

index.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>

surveycomplete.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>

Further Links: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM