最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - JavaScript Session storage variable on another page - Stack Overflow

programmeradmin0浏览0评论

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>

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>
Share Improve this question edited Jun 26, 2016 at 2:53 Hamza Zafeer 2,44613 gold badges35 silver badges47 bronze badges asked Jun 26, 2016 at 2:12 TaylorTaylor 3651 gold badge5 silver badges16 bronze badges 4
  • Opening a page in a new tab or window will cause a new session to be initiated. See more at developer.mozilla/en-US/docs/Web/API/Window/sessionStorage – Maria Ines Parnisari Commented Jun 26, 2016 at 2:16
  • Hmm, wouldn't session storage still work if 'Page 2' was opened in the same tab as Page 1, or does this also cause a new session to be initiated? – Taylor Commented Jun 26, 2016 at 2:22
  • 1 It wouldn't work. You should use local storage. – Maria Ines Parnisari Commented Jun 26, 2016 at 2:22
  • Why don't you give the variable per URL? – user2628521 Commented Jun 26, 2016 at 2:35
Add a ment  | 

3 Answers 3

Reset to default 2

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

Edit: Clearly the answer above is much better than what i provided. I'll leave this here for future viewers - maybe the way I phrased things help someone sometime.


Taylor,

Two things.

  1. sessionStorage() is unique to each page/tab. From the docs "Opening a page in a new tab or window will cause a new session to be initiated"

  2. window.onload is expecting a function. You're just concatenating a string.

If you find a different way to pass information from one page to another (you could stuff it in the URL) your new color function should look something like this:

<script>
window.onload = writeColor(sessionStorage.colorVar);

function writeColor(color) {
  document.write("Your color is " + color + "!")
}
</script>

You can't set window.onload to be a string; you have to point it directly to a function.

I would suggest creating a new function (call it writeColor) that contains if statements based on the value of sessionStorage.colorVar; then you can do window.onload=writeColor;

Alternately, change your window.onload line to window.onload = window[sessionStorage.colorVar + 'Write']; which will grab your function from the global scope and assign it to window.onload.

发布评论

评论列表(0)

  1. 暂无评论