Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
How to prevent loading multiple views when navigating fast with the back and forward button

I am working on a web application where the user can navigate between different pages (or 'partial views') which are loaded in with AJAX. I made it so when the user presses the back and forward button in the browser that the application returns the correct view depending on the url the adress bar contains on that moment.

My problem: When I navigate slowly by pressing the back and forward button in the browser it works like normal. But when I repeatedly press the back or forward button it loads multiple views within the application.

My question: Is there a way to prevent multiple views from loading in the application and only load the view which is supposed to load?

My Code:

$(window).on("popstate", function (e) {
    $(".UserNavigation li").removeClass("active");
        LoadContent.init();
    })

    var LoadContent = {
    init: function (url, query) {

        var Content = { }
        var PathName;

        if (url == null) {
            PathName = window.location.pathname;
            Content.url = "ContentArea/" + PathName;
        }    
        else {
            var Controller = "ContentArea/"
            Content.url = Controller + url
        }            

        if (query)
            Content.query = query       

        LoadContent.Loading(this);

        xhr = $.ajax({
            type: "POST",
            url: Content.url,
            data: { query: Content.query, type: 0 },
            success: function (data) {  

                LoadContent.ApplyContent(data);

            },
            error: function () {

            },
            complete: function (data) {
                if (url != null)
                    LoadContent.UpdateHistory(data, url);
            }
        })
    },

    ApplyContent: function (data) {
        $("#ContentLoading").animate({
            opacity: 0
        }, 200, function () {
            $("#ContentLoading").addClass("hidden");
            $("#ContentArea").append(data).addClass("visible");
            LoadContent.UpdateMenu();
        })

        return this;
    },

    Loading: function () {
        $("#ContentArea").children().remove();
        $("#ContentLoading").removeClass("hidden");
        $("#ContentArea").removeClass("visible");
        $("#ContentLoading").animate({
            opacity: 1
        }, 200)
    },

    UpdateHistory: function (data, url) {
        var DataToSave = {
            StateURL: url,
        }

        history.pushState(DataToSave.StateURL, url, url)
    },

    UpdateMenu: function () {
        var CurrentPath = window.location.pathname;
        $(".UserNavigation li").removeClass("active");
        $("[href='" + CurrentPath + "']").parent().addClass("active");
    }
}
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

In the following part

ApplyContent: function (data) {
    $("#ContentLoading").animate({
        opacity: 0
    }, 200, function () {
        $("#ContentLoading").addClass("hidden");
        $("#ContentArea").append(data).addClass("visible");
        LoadContent.UpdateMenu();
    })

    return this;
},

Change .append() into .html(). This will prevent the extra content from filling up more space, but completely replace the html of #ContentArea.

Like Parth said, you should also abort the running Ajax requests and start a new one. This won't prevent the server from sending you your results, so it's best to also cancel all old requests on the server when you're sending new ones.

about 4 years ago · Santiago Trujillo Report

0

In your case ajax response load data so what happen if you send multiple request,off course ajax data(view) replace old one.so you need to stop previous ajax request

 if (typeof this.xhr !== 'undefined')
       this.xhr.abort();

aboove code stop previous ajax request

 this.xhr = $.ajax({
    type: "POST",
    url: Content.url,
    data: { query: Content.query, type: 0 },
    success: function (data) {  

        LoadContent.ApplyContent(data);

    },
    error: function () {

    },
    complete: function (data) {
        if (url != null)
            LoadContent.UpdateHistory(data, url);
    }
})
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!