Home > Software design >  How do I transfer a value between webpages using local storage?
How do I transfer a value between webpages using local storage?

Time:10-05

localStorage.setItem("jobreference1", "1FN43");
<form action="apply.html" method="post">
      <div class="consultantinformation">
        <h2> Job reference number: </h2>
        <br>
        <h4 id="jobreference2"> 6LZ9W </h4>
        <input type="submit" value="Apply">
        </div>
        </form>

"use strict"

document.getElementById("job1").innerHTML = localStorage.getItem("jobreference1");
<form action="https://mercury.swin.edu.au/it000000/formtest.php" method="post" id="regform">
          <p>Job Reference Number: <span id="job1"></span>
          </p>
          </form>

I'm trying to transfer "jobreference1" from the 1st snippet to the 2nd using local storage. As you can see, I've stored it in one external JavaScript file and sent it to another. But when I try to call it in the HTML code in the 2nd snippet, to which the value was transferred to, it simply doesn't work. I've decided to include all of my HTML and JavaScript code for my second snippet because I believe that's where the issue lies. Thank you.

Note: no Inline JavaScript or jQuery.

CodePudding user response:

This is a working example. The index.html sets the reference on page load. The form submit redirects to apply.html and it reads the correct value at page load.

index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>SwinTech</title>
</head>

<body>
    <form action="apply.html" method="post">
        <div class="consultantinformation">
            <h2> Job reference number: </h2>
            <br>
            <h4 id="jobreference2"> 6LZ9W </h4>
        </div>
        <button>Submit</button>
    </form>
    <script>
        localStorage.setItem("jobreference1", "1FN43");
    </script>
</body>

</html>

apply.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <p>Job Reference Number: <span id="job1"></span></p>
    <script>
        document.getElementById("job1").innerHTML = localStorage.getItem("jobreference1");
    </script>
</body>

</html>
  • Related