I'm creating a website using jQuery $.ajax
, once I click a menu icon, an overlay opens and show the requested content based on the link href
attribute. When I do this, the URL of the site changes to whatever link you click. For example, if you click "about" the URL would be .
I don't want this, because when you close the overlay, the site stays like , and if you enter the site that way, the about page won't open, not even the overlay will open. I'm wondering if there's a way the URL doesn't change and is always
.
Here's my current jQuery code:
$(function() {
$('.w-container .w-nav-menu a').click(function() {
var $linkClicked = $(this).attr('href');
document.location.hash = $linkClicked;
var $pageRoot = $linkClicked.replace('#', '');
if (!$(this).hasClass("active")) {
$(".w-container .w-nav-menu a").removeClass("active");
$(this).addClass("active");
$.ajax({
type: "POST",
url: "load.php",
data: 'page='+$pageRoot,
dataType: "html",
success: function(msg){
if((msg))
{
$('.content').html(msg);
$('.content').hide().fadeIn();
}
}
});
}
else {
event.preventDefault();
}
});
var hash = window.location.hash;
hash = hash.replace(/^#/, '');
switch (hash) {
case 'products' :
$("#" + hash + "-link").trigger("click");
break;
case 'about' :
$("#" + hash + "-link").trigger("click");
break;
case 'storelocator' :
$("#" + hash + "-link").trigger("click");
break;
case 'media' :
$("#" + hash + "-link").trigger("click");
break;
case 'faq' :
$("#" + hash + "-link").trigger("click");
break;
case 'contact' :
$("#" + hash + "-link").trigger("click");
break;
}
});
I'm creating a website using jQuery $.ajax
, once I click a menu icon, an overlay opens and show the requested content based on the link href
attribute. When I do this, the URL of the site changes to whatever link you click. For example, if you click "about" the URL would be http://example./#about
.
I don't want this, because when you close the overlay, the site stays like http://example./#about
, and if you enter the site that way, the about page won't open, not even the overlay will open. I'm wondering if there's a way the URL doesn't change and is always http://example.
.
Here's my current jQuery code:
$(function() {
$('.w-container .w-nav-menu a').click(function() {
var $linkClicked = $(this).attr('href');
document.location.hash = $linkClicked;
var $pageRoot = $linkClicked.replace('#', '');
if (!$(this).hasClass("active")) {
$(".w-container .w-nav-menu a").removeClass("active");
$(this).addClass("active");
$.ajax({
type: "POST",
url: "load.php",
data: 'page='+$pageRoot,
dataType: "html",
success: function(msg){
if((msg))
{
$('.content').html(msg);
$('.content').hide().fadeIn();
}
}
});
}
else {
event.preventDefault();
}
});
var hash = window.location.hash;
hash = hash.replace(/^#/, '');
switch (hash) {
case 'products' :
$("#" + hash + "-link").trigger("click");
break;
case 'about' :
$("#" + hash + "-link").trigger("click");
break;
case 'storelocator' :
$("#" + hash + "-link").trigger("click");
break;
case 'media' :
$("#" + hash + "-link").trigger("click");
break;
case 'faq' :
$("#" + hash + "-link").trigger("click");
break;
case 'contact' :
$("#" + hash + "-link").trigger("click");
break;
}
});
Share
Improve this question
edited Apr 13, 2016 at 7:37
Michał Perłakowski
92.9k30 gold badges163 silver badges187 bronze badges
asked Apr 13, 2016 at 7:15
JayJay
1695 silver badges16 bronze badges
3 Answers
Reset to default 5Move event.preventDefault();
outside the else
block.
Also, remove the document.location.hash = $linkClicked;
line.
$(function() {
$('.w-container .w-nav-menu a').click(function() {
var $linkClicked = $(this).attr('href');
var $pageRoot = $linkClicked.replace('#', '');
if (!$(this).hasClass("active")) {
$(".w-container .w-nav-menu a").removeClass("active");
$(this).addClass("active");
$.ajax({
type: "POST",
url: "load.php",
data: 'page='+$pageRoot,
dataType: "html",
success: function(msg){
if((msg))
{
$('.content').html(msg);
$('.content').hide().fadeIn();
}
}
});
}
event.preventDefault();
});
var hash = window.location.hash;
hash = hash.replace(/^#/, '');
switch (hash) {
case 'products' :
$("#" + hash + "-link").trigger("click");
break;
case 'about' :
$("#" + hash + "-link").trigger("click");
break;
case 'storelocator' :
$("#" + hash + "-link").trigger("click");
break;
case 'media' :
$("#" + hash + "-link").trigger("click");
break;
case 'faq' :
$("#" + hash + "-link").trigger("click");
break;
case 'contact' :
$("#" + hash + "-link").trigger("click");
break;
}
});
document.location.hash = $linkClicked;
that line makes your url change
also use preventDefault()
to stop the link click from changing the url of the page
Use e.preventDefault();
remove all hash alteration code
$('.w-container .w-nav-menu a').click(function(e) {
e.preventDefault();