可以't将sessionStorage设置为Javascript中的一个变量
Can't set a sessionStorage to a variable in Javascript
我有一个非常长的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();
因此,你没有给它一个执行的机会。
相关文章:
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- 导航到特定事件的另一个变量页面
- 在另一个函数中使用变量this
- 我可以在Javascript/jQuery中使用一个变量作为键吗
- 使用mongodb更新中的一个变量
- 为什么window.open不打开一个以变量形式给出的链接
- 将节点数据分配给另一个变量jstree
- 如何将变量传递到另一个js文件
- 我怎么能让流星简单模式中的一个字段等于一个js变量,并且仍然让用户填写简单模式的其余部分
- 如何从onclick函数设置全局变量并将其传递给另一个JS文件
- 将会话变量从一个页面传递到另一个页面
- Javascript:在激发函数后返回一个变量以供使用
- 替换另一个变量对象中的变量值
- 什么'它的意思是当一个变量位于括号外时
- 这在JavaScript中是一个好的变量名吗
- 可以't将sessionStorage设置为Javascript中的一个变量
- 如何在javascript中给变量一个随机值
- 在不同的.js文件之间传递变量;一个是在iframe内
- Console.log(变量);VS console.log([变量]);/一个提供信息,另一个没有
- 是否可以给变量一个默认值