【发布时间】:2016-05-15 01:12:44
【问题描述】:
我有一个非常长的文件路径名列表和 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,这将使用“if”语句之一,因为已设置变量。这些 if 语句调用脚本来更改 Flash 窗口中包含的内容。
【问题讨论】:
标签: javascript jquery html