1

I tried to make some script that handle with event like "panel changes follow by URLhash". I had no problem with event on button or link but I had problem when I refreshed the page. My panel couldn't keep its values and automatically reset.

And I tried to solve refresh problem like this:

<script>
document.addEventListener('keydown', function(e) {
    if(e.which == 116) {
        window.alert("1234");
        var id = window.location.hash;
        changePanel(id);
    }
}, false);
</script>

As code I shown you, alert() function it was working, but changePanel() wasn't working. I think because alert() had ran before refreshed and changePanel() also. But in case of changePanel() it ran before refresh then after refreshed all changes are reset to default.

How to run changePanel() after refresh?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
fronthem
  • 4,011
  • 8
  • 34
  • 55
  • See http://stackoverflow.com/questions/2482059/disable-f5-and-browser-refresh-using-javascript (use `e.preventDefault()` to prevent refresh) – Alvin Wong Mar 15 '13 at 06:26

2 Answers2

0

It will not work, because after press of f5 page will be refreshed so new content will come on page, and so your panel/tab is reset..

You can try your code on

$(document).ready(
     var id = window.location.hash;
     changePanel(id);
);

Above code will automatically set tab after refresh button, and you can keep same code of that 2 lines on your button press, so that will work for your live page too. :)

This will work for you.

Thanks.

Patriks
  • 1,012
  • 1
  • 9
  • 29
0

It works perfectly for me.

Call the funtion from your html

<button type="button" name="button" onclick="loro()">fdthd</button>

JS file

function loro() {
  location.reload();
  localStorage.setItem("po", "momo");
}

$(window).on("load", function() {
if (localStorage.getItem("po") === "momo" ) {
  alert('Bien, ejecutaste el boton')
  localStorage.setItem("po", "lulu");
} else {
  alert('No has precionado el boton')
}

});

Before refresh the page I run a Function to save the "momo" value in the "po"key After that, I refresh the page and check if PO has the value of momo. Depending on if I run the function before the refresh, it will run a specific alert.

Félix Pujols
  • 107
  • 1
  • 7