JavaScript Session storage variable on another page

后端 未结 3 586
日久生厌
日久生厌 2020-12-02 01:42

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

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-02 02:01

    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.

提交回复
热议问题