私は非常に長いファイルパス名とフラッシュギャラリーのリンクを持っています。この配列は、バック、ランダム、次のボタンと相互作用します。変数 'c'をsessionStorage変数に設定しようとしています。そのため、ページがリフレッシュされたときに(理由のために必要な)値が保存されます。私はsessionStorageせずにこれらの変数をテストし、すべてがうまく動作します。しかし、注意しなければならないことは、「ランダム」ボタンが機能し、前後のボタンが機能しないことです。Javascriptの変数にsessionStorageを設定できません
私は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関数に行きます初めてのページに出たとき、ステートメントは何もせず、ユーザーがページを更新する変数を割り当てるボタンをクリックするのを待ちます。ページがリフレッシュされると、変数が設定されているので、 "if"ステートメントの1つを使用するinitを再度実行します。これらのif文は、スクリプトを呼び出して、フラッシュウィンドウ内に含まれているものを変更します。
正確にあなたがそれを宣言したが、私はどこでもあなたがinit();
に沿っため、あなたは、イベントがそれ与えていないが提供されているコードで表示されないのですか?,,初期化」機能を呼び出している
これは、sessionStorageに関する部分を除いて、すべてのコードが機能します。 'c'変数のロジックは、通常の変数として使用するだけで動作しますが、セッション変数init関数とそれ以外のものはすべて、下のイベントリスナーによって処理されます – cosmichero2025