可以't将sessionStorage设置为Javascript中的一个变量

Can't set a sessionStorage to a variable in Javascript

本文关键字:变量 一个 Javascript 设置 sessionStorage 可以      更新时间:2023-09-26

我有一个非常长的flash库文件路径名和链接列表。此数组与后退、随机和下一个按钮进行交互。我正试图将变量"c"设置为sessionStorage变量,以便在页面刷新时(出于某种原因,我要求它这样做)保存值。我在没有sessionStorage的情况下测试了这些变量,一切都很顺利。不过需要注意的一点是,"随机"按钮可以工作,而"上一步"answers"下一步"按钮不工作。

我试图转换这三个函数,但我只展示了我通过"back"函数所做的更改,这样每个人都可以了解在此之前的工作原理。

var c;
    sessionStorage.setItem('order', c);
    var flashcon, test, temp;
    var backbutt, randbutt, nextbutt;
    backbutt = document.getElementById('back');
    randbutt = document.getElementById('rand');
    nextbutt = document.getElementById('next');
    flashcon = document.getElementById('flashcon');
    function init() {
        if (sessionStorage.getItem('ref') == 1) {
            console.log('Value: ref==1(back)');
            back();
        } else if (sessionStorage.getItem('ref') == 2) {
            console.log('Value: ref==2(rand)');
            rand();
        } else if (sessionStorage.getItem('ref') == 3) {
            console.log('Value: ref==3(next)');
            next();
        } else {
            console.log('State: First session or refresh session');
        }
        // Scripts for the buttons
        backbutt.onclick = function () {
            console.log('Event: backbutton.onclick');
            sessionStorage.setItem('ref', 1);
            location.reload();
        };
        randbutt.onclick = function () {
            console.log('Event: randbutton.onclick');
            sessionStorage.setItem('ref', 2);
            location.reload();
        };
        nextbutt.onclick = function () {
            console.log('Event: nextbutton.onclick');
            sessionStorage.setItem('ref', 3);
            location.reload();
        };
    }
    // Changes the name at the top of the screen
    function displayFiles() {
        console.log('Called: displayFiles()');
        test = paths[c].substring(paths[c].lastIndexOf('.') + 1, paths[c].length);
        document.getElementById('title').innerHTML = displaytext[c];
        flashcon.innerHTML =
            '<object id="flashcontent" data="' + paths[c] + '">' +
            '<param name="movie" type="application/x-shockwave-flash">' +
            '</object>';
    }
    // What switches the flashs
    function back() {
        console.log('Called: back()');
        sessionStorage.removeItem('ref');
        if (sessionStorage.getItem('order') == 0) {
            console.log('Did the whole if thing');
            c = paths.length;
            c = sessionStorage.getItem('order');
        }
        console.log('Went past the if');
        c--;
        c = sessionStorage.getItem('order');
        displayFiles();
        download();
        console.log('Value of "c": ' + c);
    }
    function next() {
        console.log('Called: next()');
        sessionStorage.removeItem('ref');
        if (c == paths.length - 1) {
            c = -1;
        }
        c++;
        displayFiles();
        download();
        console.log('Value of "c": ' + c);
    }
    function rand() {
        console.log('Called: rand()');
        sessionStorage.removeItem('ref');
        temp = c;
        while (c == temp) {
            c = Math.floor(Math.random() * paths.length);
        }
        displayFiles();
        download();
        console.log('Value of "c": ' + c);
    }

(对我的代码做了一点解释,所以它很有意义)

当用户第一次进入页面时,它将转到init函数,该函数将转到"else"语句。该语句不执行任何操作,而是等待用户单击一个按钮,该按钮分配一个刷新页面的变量。刷新页面后,它将再次运行init,该init将使用其中一个"if"语句,因为已经设置了变量。这些if语句调用脚本来更改flash窗口中包含的内容。

你到底在哪里调用,,init"函数?你声明了它,但我在你提供的代码中看不到任何地方有

init();

因此,你没有给它一个执行的机会。