Reputation: 179
I'm getting a lot of complaints from the users that when they're using my jQuery-based tabs, they find it annoying that when they hit back on their browser, they don't go to the previous tab but to the previous page. I added the following previous/next buttons but not enough. How can I reconfigure my buttons so that users would go to the previous tab rather than the previous page when they click the browser back arrow. You can test it here.
$('#quicktabs-registration_steps').append('<div class="prevnext"><a class="tablink-prev btn" href="#">Prev</a><a class="tablink-next btn btn-lg btn-primary" href="#">Continue</a></div>');
$('.tablink-prev').click(function () {
var index = $('.quicktabs-tabs li.active').index();
$('.quicktabs-tabs li').eq(index).removeClass('active');
if (index == 0) {
index = 1;
}
$('.quicktabs-tabs li').eq(index - 1).addClass('active');
$('.quicktabs-tabs li').eq(index - 1).find('a').click();
return false;
});
$('.tablink-next').click(function () {
var length = $('.quicktabs-tabs').first().children().size();;
var index = $('.quicktabs-tabs li.active').index();
$('.quicktabs-tabs li').eq(index).removeClass('active');
// if (parseInt(index) == parseInt(length) - 1 ) {
// index = index - 1;
// }
if (parseInt(index) == parseInt(length) - 1) {
window.location.href = '/home'; //redirect to home
//index = index - 1;
}
$('.quicktabs-tabs li').eq(index + 1).addClass('active');
$('.quicktabs-tabs li').eq(index + 1).find('a').click();
return false;
});
Upvotes: 15
Views: 6943
Reputation: 1181
I have used bootsrap tabs with browser tab navigation below is the complete example
<div class="tabs-Nav border-top-0">
<ul class="nav" role="tablist">
<li>
<a
id="friends"
data-pagination-id="friends"
class="active"
data-toggle="tab"
href="#friendsTab"
role="tab"
aria-controls="friends"
aria-selected="true">
Friends
</a>
</li>
<li>
<a id="followers" data-pagination-id="followers" class="" data-toggle="tab" href="#followersTab" role="tab" aria-controls="followers" aria-selected="false">
Followers
</a>
</li>
<li>
<a id="followings" data-pagination-id="followings" class="" data-toggle="tab" href="#followingTab" role="tab" aria-controls="followings" aria-selected="false">
Following
</a>
</li>
<li>
<a id="invites" data-pagination-id="invites" class="" data-toggle="tab" href="#invitesTab" role="tab" aria-controls="invites" aria-selected="false">
Invites
</a>
</li>
</ul>
</div>
/**
* add // id="friends" data-pagination-id="friends"
* in your html tabs the demo html is also added.
**/
$(document).ready(function () {
const param = 'tabID';
$("[data-pagination-id]").click(function () {
const pageParam = $(this).attr('data-pagination-param') || param;
//add tabId to the params
addParam(pageParam, $(this).attr('data-pagination-id'), true);
});
const preTab = getParamVal(param);
if (param) {
$('[data-pagination-id="'+ preTab +'"]').click();
}
});
/**
* add params to the url if preParams is false then all previous
* params will be removed.
**/
addParam = (pageParam, paramValue, preParams) => {
//get current url without params
var mainUrl = window.location.href.split('?')[0];
//get params without url
var paramString = location.search.substring(1);
if (preParams && paramString) { //when params found
//change param string to json object
var paramsObj = JSON.parse('{"' + decodeURI(paramString).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}');
} else {
//when current params are empty then create an empty object.
var paramsObj = {};
}
//only for ads tabs
if (pageParam) {
//add tabId to the params
paramsObj[pageParam] = paramValue;
//push params without refreshing the page.
history.pushState(false, false, mainUrl + '?' + $.param(paramsObj).replace(new RegExp('%2B', 'gi'), '+'));
}
};
/**
* Get value of a params from url if not exists then return null
**/
getParamVal = (pageParam) => {
const mainUrl = window.location.href.split('?')[0];
const paramString = location.search.substring(1);
const paramsObj = JSON.parse('{"' + decodeURI(paramString).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}');
return paramsObj[pageParam];
};
You can check live working example of my code
1> GO TO https://www.notesgen.com ( use desktop )
2> Create your account as student
3> GO TO My Account / My Downloads
4> Click on My Connections
Upvotes: 2
Reputation: 434
Changing the hash is similar to pushing state history.pushState
which triggers popstate
event. Hitting back and forward on browser will pop and push these states so you won't need any other custom handler.
PS: You can add css style to :target
from your active
class. The window.addEventListener('popstate'
here just logs to show you the event but the event.state
will always be null in this case.
Run code snippet try navigating tabs then use the browser back and forward buttons.
<style type="text/css">
div { display: none }
:target { display: block }
</style>
<h2><a href="#Tab1">Tab 1</a> | <a href="#Tab2">Tab 2</a> | <a href="#Tab3">Tab 3</a> | <a href="#Tab4">Tab 4</a></h2>
<div id="Tab1">
<p>This tab 1 content</p>
</div>
<div id="Tab2">
<p>This tab 2 content</p>
</div>
<div id="Tab3">
<p>This tab 3 content</p>
</div>
<div id="Tab4">
<p>This tab 4 content</p>
</div>
<script>
(() => {
history.pushState(null, '', '#Tab1');
window.addEventListener('popstate', event => {
console.log(window.location.hash);
});
})()
</script>
Upvotes: 1
Reputation: 39
first thing that you need, prevent the link to submit the url in history by $event.preventDefualt() in click event and customize history with history.pushState(data, title, url).
In js we have an event that help us to control the back and forward event by user. This event name is "popstate". you can use it by window.addEventListener('popstate', callback). in callback function you can activate and disactivate the tab(add and remove class) and extract the url.
I show this with a sample code. for doing and understanding better, try it in a server or local server because in there changing url in the here is limited:
$('nav ul a').on('click', function($e) {
function appendContent(hash) {
$('#content').text(`Content of item ${hash} `);
}
$e.preventDefault(); // when click the link stay in page
let _this = this; // get the link tag
let hash = _this.hash.replace(/\#/, '');
appendContent(hash);
$('li').removeAttr('class');
$('a').removeAttr('class');
$(`a[href*=${hash}]`).addClass('text-white').addClass('border-0').parent().addClass('bg-primary').addClass('last-style');
history.pushState('', _this.text, hash); // add the link in history
window.addEventListener('popstate', function ($e) {
let hashAgain = location.hash.replace(/\#/, ''); // you extract the hash
appendContent(hashAgain); // set the content of the back or forward link
$('li').removeAttr('class');
$('a').removeAttr('class');
$(`a[href*=${hash}]`).addClass('text-white').addClass('border-0').parent().addClass('bg-primary').addClass('last-style'); // update the status of tab
});
});
ul li {
display: inline-block;
margin: 1em;
}
li a,
.last-style {
border: 1px solid;
padding: 0.5em 1em;
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" >
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Learning Java Script In Deep</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script type="text/javascript" src="js/angular/angular.min.js"></script>
</head>
<body>
<header class="text-center text-black-50">
<h1>Learning JS in Deep</h1>
<nav id="">
<ul id="">
<li><a href="#home" class="home">Home</a></li>
<li><a href="#item1">Item 1</a></li>
<li><a href="#item2">Item 2</a></li>
<li><a href="#item3">Item 3</a></li>
<li><a href="#item4">Item 4</a></li>
</ul>
</nav>
</header>
<div class="container m-auto text-center" id="content">
<p>Load <em class="text-danger">Content</em> in Here</p>
</div>
<script
src="https://code.jquery.com/jquery-3.4.1.js"
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</body>
</html>
Upvotes: 0
Reputation: 1195
at the one side, your next tab function is written in javascript, and at the other side, the back button in the browser uses the browser history so this is irrelevant to your functions and could not get you to the previous tab at all.
So you have two ways to make it happen:
1st: try to load each step in a different page by refreshing browser, so the page saves in browser history and by pressing back button you can see the previous step
2nd: you should have a "previous" button to see the previous step just like your next step button
Upvotes: 2
Reputation: 946
You can use History.
Push a history state when a new tab is opened
history.pushState({page: 1}, "title 1", "?page=1")
Reference: https://developer.mozilla.org/en-US/docs/Web/API/History_API
Upvotes: 13
Reputation: 34197
Without your full code I cannot give you a bespoke example but you can simply add anchor tags to each tabs containing div.
This will update the URL each time a user clicks a tab link. These URL changes will be stored in the browser history and will be recalled when navigating backwards.
<div id="Tab1">
<h2><a href="#Tab1">Tab 1</a></h2>
<p>This tab content</p>
</div>
<div id="Tab2">
<h2><a href="#Tab2">Tab 2</a></h2>
<p>This tab content</p>
</div>
<div id="Tab3">
<h2><a href="#Tab3">Tab 3</a></h2>
<p>This tab content</p>
</div>
<div id="Tab4">
<h2><a href="#Tab4">Tab 4</a></h2>
<p>This tab content</p>
</div>
The updated URL will look like this each time you click a tab:
https://example.com#Tab1
https://example.com#Tab2
https://example.com#Tab3
https://example.com#Tab4
Upvotes: 7