最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Do not change URL when doing a call with AJAX - Stack Overflow

programmeradmin4浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 5

Move 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();
发布评论

评论列表(0)

  1. 暂无评论