2016-05-14 18 views
0

我有一个非常长的文件路径名和一个闪存画廊链接的列表。这个数组与后面,随机和下一个按钮交互。我想将变量“C”设置为可变的sessionStorage所以当页面refreshs(我需要它的理由这样做)的值保存。我已经测试了这些变量,没有sessionStorage,一切都很正常。需要注意的一点是,“随机”按钮可以工作,而后退和下一个按钮不起作用。不能sessionStorage的一个设置为变量在Javascript

我试图把所有3条,但我只表明我与“后退”功能更改,因此每个人都可以得到此之前,什么工作的透视。

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将使用“如果” statments之一,因为变量已设置。这些if语句调用脚本来更改Flash窗口中包含的内容。

回答

0

确切位置在哪里,你调用初始化,,”功能吗?你声明,但我没有看到在代码的任何地方你

init(); 

所具有的管线,因此你不事件给它一个机会执行。

+0

它执行我的代码是除了有关sessionStorage的部分功能。“C”类变量的逻辑所有的作品时,我只是用它作为一个普通的变量,但我试图将其转换会话变量所以一切都能正常工作,init函数和其他所有的东西都由底层的eventlistener处理 – cosmichero2025