繁体   English   中英

无法将sessionStorage设置为Javascript中的变量

[英]Can't set a sessionStorage to a variable in Javascript

我的Flash画廊的文件路径名和链接列表非常长。 该数组与后退,随机和下一步按钮进行交互。 我正在尝试将变量'c'设置为sessionStorage变量,以便在页面刷新时(出于某些原因我要求这样做)保存该值。 我已经在没有sessionStorage的情况下测试了这些变量,并且一切正常。 不过要注意的一件事是,“随机”按钮有效,而后退和下一步按钮无效。

我尝试转换所有三个,但只显示了使用“后退”功能所做的更改,因此每个人都可以了解在此之前有效的方法。

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();

因此,您没有事件给它执行的机会。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM