Having difficulty loading external page within an active session containing local storage in order to resume last activity after refreshing

Query regarding Bounty:

I am working with Bootstrap-4 tabs and need to load an external page within the active tab. Additionally, I want the page to return to the last active tab when refreshed.

Using Bootstrap navs, my aim was to load an external page inside the active div. I utilized localstorage to store the last active tab so that it can be reactivated.

Furthermore, to address the delay in loading external pages, I implemented a bootstrap spinner. Below is the code snippet:

$(document).ready(function() {
  $('a[data-toggle="tab"]').on('shown.bs.tab', function(e) {
    localStorage.setItem('activeTab', $(e.target).attr('href'));
    var $this = $(this),
      loadurl = $this.attr('href'),
      targ = $this.attr('data-target');
    $.get(loadurl, function(data) {
      $(targ).html(data);
    });
    $this.tab('show');
    $("#tabsp").hide();
    return false;
  });

  var activeTab = localStorage.getItem('activeTab');
  if (activeTab) {
    $('.nav-tabs a[href="' + activeTab + '"]').tab('show');
    var $this = $(this),
      loadurl = $this.attr('href'),
      targ = $this.attr('data-target');
    $.get(loadurl, function(data) {
      $(targ).html(data);
    });
    $this.tab('show');
    $("#tabsp").hide();
    return false;
  }
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.0/js/bootstrap.min.js"></script>
<ul class="nav nav-tabs" id="myTab" role="tablist">
  <li class="nav-item">
    <a href="/dashboard/proinfo.php" data-target="#home" class="nav-link active" id="contacts_tab" data-toggle="tab"> Info </a>
  </li>
  <li class="nav-item">
    <a href="/dashboard/ads.php" data-target="#ads" class="nav-link" id="ads-tab" data-toggle="tab"> Ads </a>
  </li>
  <li class="nav-item">
    <a href="/dashboard/favor.php" data-target="#fav" class="nav-link" id="fav-tab" data-toggle="tab"> Favorites </a>
  </li>
  <li class="nav-item">
    <a href="/dashboard/sold.php" data-target="#sol" class="nav-link" id="sol-tab" data-toggle="tab"> Sold </a>
  </li>
</ul>

<div class="tab-content profile-tab" id="myTabContent">
  <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="contacts_tab">

  </div>
  <div class="tab-pane fade" id="ads" role="tabpanel" aria-labelledby="ads-tab">

  </div>
  <div class="tab-pane fade" id="fav" role="tabpanel" aria-labelledby="fav-tab">

  </div>
  <div class="tab-pane fade" id="sol" role="tabpanel" aria-labelledby="sol-tab">

  </div>
  <div class="text-center" id="tabsp">
    <div class="spinner-border text-primary" role="status">
      <span class="sr-only">Loading...</span>
    </div>
  </div>
</div>

To summarize, the elements I aimed to include were:

  • Loading an external page within a div
  • Returning to the last active tab on refresh using local storage
  • Incorporating a bootstrap spinner for better user experience during loading

When initially accessing index_file.php, only the bootstrap spinner is visible, indicating the absence of localstorage.

Seeking guidance on how to resolve this issue?

Answer №1

This might be the solution you need, unfortunately, I haven't been able to fully test it.

var defaultTab = 'contacts_tab';
$(document).ready(function() {
  $('a[data-toggle="tab"]').on('shown.bs.tab', function(e) {
    var tabEl = $(e.target);
    saveIntoLocalStorage(tabEl.attr('id'));
    loadPage(tabEl);
  });
  var tabToActivate = loadFromLocalStorage();
  loadPage(tabToActivate);
});

function loadFromLocalStorage() {
  var activeTab = localStorage.getItem('activeTab');
  if (activeTab) {
    return $("#" + activeTab);
  } else {
    saveIntoLocalStorage(defaultTab);
    return $("#" + defaultTab);
  }
}

function saveIntoLocalStorage(id) {
  localStorage.setItem('activeTab', id);
}

function loadPage(tabEl) {
  debugger;
  var $this = $(this),
    loadurl = tabEl.attr('href'),
    targ = tabEl.attr('data-target');
  $.get(loadurl, function(data) {
    $(targ).html(data);
  });
  $this.tab('show');
  $("#tabsp").hide();
  return false;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.0/js/bootstrap.min.js"></script>
<ul class="nav nav-tabs" id="myTab" role="tablist">
  <li class="nav-item">
    <a href="/dashboard/proinfo.php" data-target="#home" class="nav-link active" id="contacts_tab" data-toggle="tab"> Info </a>
  </li>
  <li class="nav-item">
    <a href="/dashboard/ads.php" data-target="#ads" class="nav-link" id="ads-tab" data-toggle="tab"> Ads </a>
  </li>
  <li class="nav-item">
    <a href="/dashboard/favor.php" data-target="#fav" class="nav-link" id="fav-tab" data-toggle="tab"> Favorites </a>
  </li>
  <li class="nav-item">
    <a href="/dashboard/sold.php" data-target="#sol" class="nav-link" id="sol-tab" data-toggle="tab"> Sold </a>
  </li>
</ul>



<div class="tab-content profile-tab" id="myTabContent">
  <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="contacts_tab">

  </div>
  <div class="tab-pane fade" id="ads" role="tabpanel" aria-labelledby="ads-tab">

  </div>
  <div class="tab-pane fade" id="fav" role="tabpanel" aria-labelledby="fav-tab">

  </div>
  <div class="tab-pane fade" id="sol" role="tabpanel" aria-labelledby="sol-tab">

  </div>
  <div class="text-center" id="tabsp">
    <div class="spinner-border text-primary" role="status">
      <span class="sr-only">Loading...</span>
    </div>
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What steps can I take to ensure that the information in my Cart remains consistent over time?

I recently built a NextJS application integrating the ShopifyBuy SDK. My implementation successfully fetches products from the store and displays them to users. Users can navigate to product pages and add items to their cart. However, I encountered an iss ...

Unable to trigger Bootstrap 4 dropdown in the correct position

https://i.sstatic.net/eJbUO.png Hey there, I'm currently working with Laravel and attempting to display a Bootstrap dropdown from the file a.blade.php to the file b.blade.php. The dropdown is rendering in the wrong position. You can see the attache ...

Identifying HTTP Live Streaming on mobile devices

I am seeking a reliable method to determine if a mobile device can play HTTP Live Streaming (m3u8). Currently, I am using the following script for testing purposes: function isHLSEnabled() { var videoElement = document.createElement('video' ...

Updating of an Angular Directive within a nested Directive ceases once the inner Directive has been modified

Encountered a challenge with directives nested within each other in AngularJS. The issue is that both directives share the same property through a service and have an input to modify this property. The outer directive uses "transclude" to include the inner ...

The output of new Date() varies between app.js and ejs

app.get("/test",function(req,res){ var d = new Date(); res.send(d); }); When I access mydomain/test, it displays the output "2019-03-19T04:50:47.710Z" which is in UTC. app.get("/testejs",function(req,res){ res.render("testejs");}); Below is the content ...

Strategies for transferring data from JavaScript to Express for seamless integration as a default value

I am currently utilizing express, MongoDB for the database, and EJS. I am in the process of developing a form that allows users to submit a new article for a blog. The goal is for the website to redirect back to the page with the form fields populated if a ...

Unravel the encoded string to enable JSON parsing

Here is an example of my JSON string structure [{&#034;id&#034;:0,&#034;nextCallMills&#034;:0,&#034;delay&#034;:0,&#034;start&#034;:&#034;... I am facing an issue with JSON.parseString() Even after trying unescape() a ...

Static IP Address Proxy for Inbound Traffic in App Engine and Cloud Environments

Many cloud platforms, including App Engine, utilize a diverse set of IP addresses, posing challenges for users with firewall restrictions. We have clients interested in our services but they are limited to sending requests to specific IP addresses. Is th ...

Setting the mesh size to perfectly match the viewport height and screen dimensions in three.js - here's how!

Check out this Codesandbox demo for a canvas lighting experiment: https://codesandbox.io/s/canvas-lighting-experiment-wip-3p9im?file=/src/Canvas.component.jsx Although I've adjusted the canvas to fit the height and width of the window, the mesh withi ...

Exploring Next.js: A Guide to Implementing Browsing History in Your Application

Struggling to add entries to browser history when using Next.js's Link property for page navigation. Unable to push history entry, leading to incorrect page location in my application when going back. Any ideas on implementing this feature in Next.js? ...

Tips for invoking an Android function from an AngularJS directive?

I am facing an issue with my HTML that uses an AngularJS directive. This HTML file is being used in an Android WebView, and I want to be able to call an Android method from this directive (Learn how to call Android method from JS here). Below is the code ...

Looking to update the button's HTML display according to the chosen variant selected

I've implemented a feature to deactivate the add to cart button on Shopify when the inventory is available. The button works as expected, but I want to change the message to 'sold out' instead of 'add to cart' and disable it. Belo ...

javascript conceal a div when clicked elsewhere

I am trying to implement two JavaScript functions: 1) Displaying a DIV (welcomeDiv) when clicking on a button; 2) Hiding the div by clicking outside of 'welcomeDiv' container. function showDiv() { document.getElementById('welcomeDi ...

Ways to retrieve JSON string from responsetext in JavaScript

After spending the entire day on this, I still can't figure it out. I have AJAX that fetches a JSON string from a PHP script, but now I need to work with it in JavaScript. Here's what I've tried: var xmlhttp; xmlhttp=new XMLHttpRequest(); ...

Is there a way to declare the different types of var id along with its properties in Typescript?

I recently received a task to convert a JavaScript file to a TypeScript file. One issue I am currently facing is whether or not I should define types for the 'id' with this expression, e.g., id={id}. So far, I have tried: Even though I defined ...

Any ideas on how to potentially establish a default route based on conditions with react-router-dom v6?

Managing two pages, Page1 and Page2, requires conditional configuration for setting one as the homepage based on a specific condition. Additionally, all URLs must be prefixed with an ID. Take a look at the code snippet below: <Routes> <Route pat ...

Is there a better method to organize an Array of Objects?

Greetings my fellow developers, I’m looking to rearrange the Array of objects (array1) to match the order provided in array2. As you can see, array2 is a simple array with values that correspond to key names in array1, making it the reference for organi ...

Tracking user sessions using cookies, not relying on JavaScript like Google does

Currently, I am working in an environment that utilizes PHP on the server side and Javascript on the client side. In order to monitor user sessions, I regularly send a JS PUT request to the server every 5 seconds. This allows me to gather data such as the ...

Looping through the json resulted in receiving a null value

When working with typescript/javascript, I encountered an issue while trying to fetch the 'statute' from a data object: {_id: "31ad2", x: 21.29, y: -157.81, law: "290-11",....} I attempted to assign data.law to a variable, but received a typeer ...

JQuery method for extracting a specific span's content from a div

I am faced with extracting specific text from a span within a div element. Below is the code snippet for my Div: '<div class="dvDynamic_' + pid + '"><p hidden="true">'+pid+'</p><span class="count_' + pid ...