0

I have a page made with Bootstrap 3 framework that contains two tabs. I have a button which when clicked opens the second of the two tabs. This works fine but now I want the entire page to reload after the button is clicked and then open the second tab it does not work (the page just reloads and stays on the first tab. Why does this not work?

//This works
$('button').click(function(){
  $('a[href="#credits"]').tab('show');
});



 //This doesn't work
    $('button').click(function(){
//reload entire page
      location.reload();
      $('a[href="#credits"]').tab('show');
    });
JulianJ
  • 1,259
  • 3
  • 22
  • 52
  • You will need to track some form of state, you could use a cookie for instance to remember which tab was opened last, you could append something to the url `?tab=1` for example – Dale Aug 22 '18 at 08:07
  • 2
    well after you reload the js script does not continue. So the `$('a[href="#credits"]').tab('show');` part is basically unreachable. Try to reload as last statement and it should work. – Erch Aug 22 '18 at 08:08
  • 2
    Any code below location.reload() will never execute because the page has already reloaded and any changes you do before that will get discarded. – Abana Clara Aug 22 '18 at 08:09
  • 1
    See here: https://stackoverflow.com/questions/18999501/bootstrap-3-keep-selected-tab-on-page-refresh – marcramser Aug 22 '18 at 08:09
  • The jquery code reloads the page so your code doesnt even reach `$('a[href="#credits"]').tab('show');` because it reloads on the line before that. Try researching `localStorage` and put a variable there – Simeon Nakov Aug 22 '18 at 08:10

1 Answers1

0

When you call 'reload' it's like you request the page for the first time.

When I had a similar problem, I ended up setting a cookie, then I could read that cookie to determine whether it's the first load or a reload. Hope you can use that idea.

Poul Bak
  • 10,450
  • 5
  • 32
  • 57