Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
619 views
in Technique[技术] by (71.8m points)

html - JavaScript Session storage variable on another page

So I'm rather new to JavaScript and I would love some help getting this code to work. I've looked at multiple other posts talking about session storage as well as if/else statements and still can't seem to figure it out.

I have a page, lets call it page 1 and it has 3 links, "red" "green" and "blue".

When you click on any of these links its function sets a session storage variable 'colorVar' to the color chosen and then redirects to a page called page 2.

As page 2 loads, the window.onload action is used to start a function according to the variable set on page 1. In this case the function that starts on page 2 simply displays "Your color is ____!".

Heres the code:

<!-- [This is Page 1] -->

<a href="Page2.html" onclick="colorRed()">Set color to red</a>
<a href="Page2.html" onclick="colorBlue()">Set color to blue</a>
<a href="Page2.html" onclick="colorGreen()">Set color to green</a>

<script>

function colorRed() {
      sessionStorage.setItem("colorVar", "red"); 
}
function colorBlue() {
     sessionStorage.setItem("colorVar", "blue");
}
function colorGreen() {
     sessionStorage.setItem("colorVar", "green"); 
}
</script>

<!-- [This is Page 2] -->

<script>
window.onload = sessionStorage.colorVar + 'Write()';

function redWrite() {
    document.write("Your color is red!")
}

function blueWrite() {
    document.write("Your color is blue!")
}

function greenWrite() {
   document.write("Your color is green!")
}
</script>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can pass sessionStorage as as query string at href of <a> element; use location.search at window.onload event at Page2.html

Page1.html

<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="style.css">
  <script src="script.js"></script>
</head>

<body>
  <a href="Page2.html" data-color="red" onclick="color(this)">Set color to red</a>
  <a href="Page2.html" data-color="blue" onclick="color(this)">Set color to blue</a>
  <a href="Page2.html" data-color="green" onclick="color(this)">Set color to green</a>

  <script>
    function color(elem) {
      event.preventDefault();
      sessionStorage.setItem("colorVar", elem.dataset.color);
      location.href = elem.href + "?colorVar=" + sessionStorage.getItem("colorVar");
    }
  </script>
</body>

</html>

at Page2.html

<script>
window.onload = function() {
  document.write("Your color is " + location.search.split("=").pop())
}
</script>

plnkr http://plnkr.co/edit/eNVXr4ElXRzrxlZ7EY0a?p=preview


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...