如何在javascript中点击按钮重新加载网页,但在重新加载后调用函数

How to reload a webpage with a button click in javascript but after reload calling a function

本文关键字:加载 新加载 调用 函数 网页 javascript 按钮      更新时间:2023-09-26

我正在制作一个闪光灯库,我希望每次在页面上显示新的闪光灯时都能重新加载页面。我想这样做是因为我想增加每次用户访问显示的广告量。该按钮点击成功地重新加载页面,但阻止了未来代码的激发,这就是显示闪光灯的原因。

var c = 0;
var flashcon, test, temp;
// Function for inital flash page to work properly
function init() {
    flashcon = document.getElementById('flashcon');
    // Scripts for the buttons
    document.getElementById('back').onclick = function () {
        location.reload(false);
        if (c == 0) {
            c = paths.length;
        }
        c--;
        displayFiles();
        download();
    }
    document.getElementById('next').onclick = function () {
        location.reload(false);
        if (c == paths.length - 1) {
            c = -1;
        }
        c++;
        displayFiles();
        download();
    }
    document.getElementById('rand').onclick = function () {
        location.reload(false);
        temp = c;
        while (c == temp) {
            c = Math.floor(Math.random() * paths.length);
        }
        displayFiles();
        download();
    }
    // Scripts for the left and right arrow key functionality
    document.addEventListener('keydown', function (e) {
        if (e.which == 37) {
            $("#back").click();
        }
    });
    document.addEventListener('keydown', function (e) {
        if (e.which === 39) {
            $("#next").click();
        }
    });
}

在重载之前在localStorage中设置一个标志,然后在init上检查值并正常调用函数或init。

您需要存储一个变量。这可能是您想要的:https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API