Javascript 比较 sessionStorage 值时无法调用函数
Javascript function not being able to call when sessionStorage value is compared
我目前正在创建一个 Flash 画廊,每次按下新按钮以查看新的 Flash 时,我都希望看到页面重新加载以显示新广告。我已经解决了 console.logs 的问题,并确定 sessionStorage 变量设置正确,但是当需要调用 back/rand/next() 函数时,它就停止了。任何提示都会很有价值。
(编辑) 它到达 console.log('Rand button ses received!');
代码
var flashcon, test, temp;
var back, rand, next;
back = document.getElementById('back');
rand = document.getElementById('rand');
next = document.getElementById('next');
flashcon = document.getElementById('flashcon');
function init() {
if (sessionStorage.getItem('ref') == 1) {
console.log('Back button ses received!');
back();
} else if (sessionStorage.getItem('ref') == 2) {
console.log('Rand button ses received!');
rand();
} else if (sessionStorage.getItem('ref') == 3) {
console.log('Next button ses received!');
next();
} else {
console.log('First time or a reset');
}
// Scripts for the buttons
back.onclick = function () {
console.log('Back button pressed');
sessionStorage.setItem('ref', 1);
location.reload();
};
rand.onclick = function () {
console.log('Rand button pressed');
sessionStorage.setItem('ref', 2);
location.reload();
};
next.onclick = function () {
console.log('Next button pressed');
sessionStorage.setItem('ref', 3);
location.reload();
};
}
// What switches the flashs
function back() {
console.log('Back function');
sessionStorage.clear();
if (c == 0) {
c = paths.length;
}
c--;
displayFiles();
download();
}
function rand() {
console.log('Rand function');
sessionStorage.clear();
if (c == paths.length - 1) {
c = -1;
}
c++;
displayFiles();
download();
}
function next() {
console.log('Next function');
sessionStorage.clear();
temp = c;
while (c == temp) {
c = Math.floor(Math.random() * paths.length);
}
displayFiles();
download();
}
您的变量和函数覆盖了彼此的名称。
back, next, rand
是所有函数语句,它们被提升到相应变量名称的 ... = document.getElementById('...')
之上。
更改函数名称或更改元素对象名称,然后重试。
所以基本上是这样转的:
var back, rand, next;
back = document.getElementById('back');
rand = document.getElementById('rand');
next = document.getElementById('next');
进入这个:
var $back, $rand, $next;
$back = document.getElementById('back');
$rand = document.getElementById('rand');
$next = document.getElementById('next');
当然还要编辑代码中的匹配位置,使它们指向正确的变量。
我目前正在创建一个 Flash 画廊,每次按下新按钮以查看新的 Flash 时,我都希望看到页面重新加载以显示新广告。我已经解决了 console.logs 的问题,并确定 sessionStorage 变量设置正确,但是当需要调用 back/rand/next() 函数时,它就停止了。任何提示都会很有价值。
(编辑) 它到达 console.log('Rand button ses received!');
代码
var flashcon, test, temp;
var back, rand, next;
back = document.getElementById('back');
rand = document.getElementById('rand');
next = document.getElementById('next');
flashcon = document.getElementById('flashcon');
function init() {
if (sessionStorage.getItem('ref') == 1) {
console.log('Back button ses received!');
back();
} else if (sessionStorage.getItem('ref') == 2) {
console.log('Rand button ses received!');
rand();
} else if (sessionStorage.getItem('ref') == 3) {
console.log('Next button ses received!');
next();
} else {
console.log('First time or a reset');
}
// Scripts for the buttons
back.onclick = function () {
console.log('Back button pressed');
sessionStorage.setItem('ref', 1);
location.reload();
};
rand.onclick = function () {
console.log('Rand button pressed');
sessionStorage.setItem('ref', 2);
location.reload();
};
next.onclick = function () {
console.log('Next button pressed');
sessionStorage.setItem('ref', 3);
location.reload();
};
}
// What switches the flashs
function back() {
console.log('Back function');
sessionStorage.clear();
if (c == 0) {
c = paths.length;
}
c--;
displayFiles();
download();
}
function rand() {
console.log('Rand function');
sessionStorage.clear();
if (c == paths.length - 1) {
c = -1;
}
c++;
displayFiles();
download();
}
function next() {
console.log('Next function');
sessionStorage.clear();
temp = c;
while (c == temp) {
c = Math.floor(Math.random() * paths.length);
}
displayFiles();
download();
}
您的变量和函数覆盖了彼此的名称。
back, next, rand
是所有函数语句,它们被提升到相应变量名称的 ... = document.getElementById('...')
之上。
更改函数名称或更改元素对象名称,然后重试。
所以基本上是这样转的:
var back, rand, next;
back = document.getElementById('back');
rand = document.getElementById('rand');
next = document.getElementById('next');
进入这个:
var $back, $rand, $next;
$back = document.getElementById('back');
$rand = document.getElementById('rand');
$next = document.getElementById('next');
当然还要编辑代码中的匹配位置,使它们指向正确的变量。