1

I am making a flash gallery and I would like the page to reload every time a new flash is displayed on the page. I would like to do this because I would like to increase the amount of ads displayed per user visit. The button clicks successfully reload the page but stops the future code from firing which is what displays the flash.

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();
        }
    });
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
cosmichero2025
  • 1,029
  • 4
  • 14
  • 37

2 Answers2

2

Set a flag in localStorage before reload Then on init check the value and call your function or init normally.

axrami
  • 177
  • 7
  • I'm unfamiliar with how to do that do you have any links for reading materials for it I would be happy to give it a read. – cosmichero2025 May 10 '16 at 21:03
  • this should help http://stackoverflow.com/questions/3262605/html5-localstorage-check-if-item-is-set – axrami May 10 '16 at 21:14
  • So I should set the value to one that automatically triggers a function then when that function is triggered it resets that value to 0 untill the button is selected again – cosmichero2025 May 10 '16 at 21:14
  • There should an if statement in your init that checks the value of localStorage if there is a value then call the function you wanted to call. If there is not then init normally. Secondly right before you reload the page you set a value in localStorage so when init is hit on the reload it looks for the value there it will execute the function you want. – axrami May 10 '16 at 21:26
1

You'll need to store a variable. This is probably what you want: https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API

Tim
  • 36
  • 5