无法在Javascript

时间:2016-05-14 15:38:18

标签: javascript jquery html

我有一个非常长的文件路径名列表和Flash库链接。该数组与后退,随机和下一个按钮进行交互。我想设置变量' c'到sessionStorage变量,所以当页面刷新时(由于原因我要求它执行),值将被保存。我已经在没有sessionStorage的情况下测试了这些变量,一切都运行得很好。有一点需要注意的是"随机"按钮工作,而后退和下一个按钮不起作用。

我试图转换所有这三个,但我只是通过' 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函数,该函数将转到&#34; else&#34;声明此语句不执行任何操作,并等待用户单击一个按钮,该按钮分配一个刷新页面的变量。刷新页面后,它将再次运行init,它将使用&#34; if&#34;自设置变量以来的语句。这些if语句调用脚本来更改flash窗口中包含的内容。

1 个答案:

答案 0 :(得分:0)

你究竟在哪里调用,init&#34;功能?你声明了它但我在代码中的任何地方都没有看到

init();
因此,你没有事件给它一个执行的机会。

相关问题