I developed a navigation bar. I just add an active class for links that highlight the current link. The strange thing is that the feature is working on my local pc perfectly. I just uploaded it to my server and check, but it's not working at all.
This is the uploaded site
This is the script that I used.
$(function () {
var url = window.location;
$('ul.active-link a[href="' + url + '"]').parent().addClass('active');
$('ul.active-link a').filter(function () {
return this.href == url;
}).parent().addClass('active');
});
<script>
$(document).ready(function () {
var location = window.location.href;
$('#nav li a').each(function(){
if(location.indexOf(this.href)>-1) {
$(this).parent().addClass('active');
}
});
});
Related
I am working on a wordpress website, all pages are loaded with Ajax.js.
Everything is working, changing pages without loading all of the website, changing the url after click…
but ONE problem remain : when I click on « go back » on my browser, the active class link is ALWAYS the first page name.
Where does that come from ?
Here is my a part of my code
$(function(){
$(window).bind('popstate', function() {
var url = window.location;
var hash = url.href.substring(url.href.lastIndexOf('/') + 1);
$('#contenu').load( url + ' #contenu');
$('#menu li').removeClass('active');
if(hash.length == 0) {
$('#menu li').first().addClass('active');
} else {
$('#menu li.'+hash+'').addClass('active');
}
});
$('#loading').hide();
$('#menu a').click(function(e){
e.preventDefault();
$('#loading').slideDown();
$('#menu li').removeClass('active');
$(this).parent().addClass('active');
var lien = $(this).attr('href');
$('#contenu').slideUp(500, function() {
$('#contenu').load( lien + ' #contenu', function(){
$('#contenu').slideDown(500, function(){
$('#loading').slideUp();
history.pushState(null, null, lien);
});
});
}); }); });
And an image
I have a sidebar nav bar in my project and I have to apply the class="active" to every href link dynamically I applied so many jquery/javasctipt function to it but its not working and its a Groovy server page(gsp).
So far what I tried
var activeurl = window.location;
$('#nav a[href="'+activeurl+'"]').parent('li').addClass('active');
2nd one
$(function(){
var url = window.location.pathname,
urlRegExp = new RegExp(url.replace(/\/$/,'') + "$"); // create regexp to match current url pathname and remove trailing slash if present as it could collide with the link in navigation in case trailing slash wasn't present there
// now grab every link from the navigation
$('#nav a').each(function(){
// and test its normalized href against the url pathname regexp
if(urlRegExp.test(this.href.replace(/\/$/,''))){
$(this).addClass('active');
}
});
});
3rd one
$("#nav a").each(function() {
if (this.href == window.location.href) {
$(this).addClass("active");
}
});
I'm using following code for my bootstrap tabs in order to have the #name of the tab in the url, as I want to redirect to a specific tab once some post actions are done, which works fine.
However, ever since I started using the code, all the other links stopped doing anything, it doesn't open the page or display an error, nothing at all.
Here's my code :
$(function() {
var gotoHashTab = function (customHash) {
var hash = customHash || location.hash;
var hashPieces = hash.split('?'),
activeTab = $('[href=' + hashPieces[0] + ']');
activeTab && activeTab.tab('show');
}
// onready go to the tab requested in the page hash
gotoHashTab();
// when the nav item is selected update the page hash
$('.nav a').on('shown.bs.tab', function (e) {
window.location.hash = e.target.hash
//location.hash = $(e.target).attr('href').substr(1);
scrollTo(0,0);
})
// when a link within a tab is clicked, go to the tab requested
$('.tab-pane a').click(function (event) {
if (event.target.hash) {
e.preventDefault();
gotoHashTab(event.target.hash);
}
})
$('.tab-pane a').on('click', function(e){
e.preventDefault()
})
});
I've noticed on some other questions, that e.preventDefault() may cause that, but when I comment those out nothing happens.
How to rewrite the code to keep functionality but make those normal links work again ?
It's looking at all the links with this:
activeTab = $('[href=' + hashPieces[0] + ']');
Change to:
activeTab = $('.tab-pane [href=' + hashPieces[0] + ']');
And also try being more specific with the .nav, since .nav is default for all navs. So this:
$('.nav a').on('shown.bs.tab', function (e) {
Should change to:
$('.tab-pane a').on('shown.bs.tab', function (e) {
I have HTML
<div id="top" class="shadow">
<ul class="gprc">
<li>Home</li>
<li>Text1</li>
<li>Text2</li>
<li>Text3</li>
<li>Text4</li>
</ul>
</div>
and JQUERY
$(function () {
var url = window.location.pathname,
urlRegExp = new RegExp(url.replace(/\/$/, '') + "$");
$('#top a').each(function () {
if (urlRegExp.test(this.href.replace(/\/$/, ''))) {
$(this).addClass('active');
}
});
});
The problem is that when i click on the Home link all tabs are getting active class and don't understand why. I need it for the first link to not get any active class.
I'm also looking for this solution and I have tested your code. On the first approach all links are highlighted and when I click other links it is working properly. The problem was on the home page because all links are highlighted because there is "no event been received" when the page is loaded, the code will work if you send a command or by clicking each links, theoretically. To stop this behavior, I found this code to one of the answers above, add this code and change the ".sibling()" to ".previousSibling()"
$(this).parent().sibling().find('a').removeClass('active');
".sibling()" will highlighted at the end of your links change it to ".previousSibling()" so it will go to first (Li)
$(this).parent().previoussibling().find('a').removeClass('active');
Your code will be like this:
$(function () {
var url = window.location.pathname,
urlRegExp = new RegExp(url.replace(/\/$/, '') + "$");
$('#top a').each(function () {
if (urlRegExp.test(this.href.replace(/\/$/, ''))) {
$(this).addClass('active');
$(this).parent().previoussibling().find('a').removeClass('active');
}
});
});
Check this , this will only activates clicked tab , remove active for all and then add for the one clicked
$("#top a").click(function() {
$('a').removeClass('active');
$(this).addClass("active");
});
Check this
You may try this out. It will help you:
<script type="text/javascript">
$(function () {
$('#sidebar li a').each(function () {
var path = window.location.pathname;
if (path.indexOf('?') > 0) {
var current = path.indexOf('?');
}
else {
var current = path;
}
var url = $(this).attr('href');
var currenturl = url.substring(url.lastIndexOf('.') + 1);
if (currenturl.toLowerCase() == current.toLowerCase()) {
$(this).addClass('active');
var par = $(this).parent();
par.addClass('open');
}
});
});
</script>
You're running a foreach loop on all <a> tags within your #top div. So of course it'll add the class active to all of them.
I think what you're trying to do is this: http://jsfiddle.net/rLddf/4/
I used the click event instead.
edit switched link to Kristof Feys example - more efficient.
try this...
$(function() {
$('#yourMenu a').each(function(){
$current = location.href;
$target= $(this).attr('href');
if ( $target== $current)
{
$(this).addClass('active');
}
});
});
I came across the same issue and I found it easier to just add an additional if statement so that the function would fire on all pages except the home page which met my needs.
$(function(){
var url = window.location.pathname,
urlRegExp = new RegExp(url.replace(/\/$/,'') + "$");
$('#top a').each(function(){
if ( window.location.pathname != '/' ){
if(urlRegExp.test(this.href.replace(/\/$/,''))){
$(this).addClass('active');
}
}
});
});
You could also add in an else statement to show an active link on the homepage by targeting the link directly if required.
I think you need something like this:
$(function () {
$("#top a").click(function(e){
e.preventDefault();
$(this).addClass('active');
$(this).parent().siblings().find('a').removeClass('active');
});
});
Fiddle Demo
I have a simple tabbed interface that switches between hidden div and sets an active state on the appropriate tab when clicked.
When someone visits the url of the tab, it shows the contents of that tab only, which is fine.
What I need it to do is also set the active state of that tab if you visited that url directly.
I've got url's such as abc.com/index.html#gallery, abc.com/index.html#recipes etc.
So if you visit abc.com/index.html#recipes - the recipes tab is highlighted.
My code is below, thanks in advance for any advice!
$(function () {
var app = {
vars: {
gallery: $('#gallery'),
tabContent: $('.tabs .tabContent'),
nav: $('.tabs nav a')
},
events: function () {
//tabs
app.vars.nav.on('click', function (e) {
var thisHash = $(this).attr('href');
app.setHash(thisHash);
e.preventDefault();
$('nav a').removeClass('active');
$(this).addClass('active');
});
//hashchange
$(window).on('hashchange', function() {
app.checkHash();
});
},
checkHash: function () {
var hash = app.getHash();
if (hash == '') {
app.vars.tabContent.hide();
app.vars.gallery.show();
} else {
app.goTo(hash);
}
},
getHash: function () {
return window.location.hash;
},
setHash: function (id) {
window.location.hash = id;
},
goTo: function (id) {
app.vars.tabContent.hide();
$(id).fadeIn();
}
}
app.events();
app.checkHash();
});
You need to amend the goTo function to set the active class on the relevant tab. Try this:
goTo: function (id) {
app.vars.tabContent.hide();
$(id).fadeIn();
$('nav a[href="' + id + '"]').addClass('active').siblings().removeClass('active');
}