Home > Software engineering >  Prevent Javascript code runing multiple times caused by caching
Prevent Javascript code runing multiple times caused by caching

Time:02-10

I have an Asp.net-MVC that when I send the request it returns a partial-view.

public ActionResult AccountDetails()
{
    return View("~/Views/partials/Account/Details.cshtml");
}

Then I load partial in my form using code below.

var condition = true;
var pageNumber = 0;
for (var i = 1; i < 10; i  ) {
    $("#page_view"   i).addClass("d-none");
    if (condition && $("#page_view"   i).html().trim() == "") {
        pageNumber = i;
        condition = false;
    }
}
$("#page_view"   pageNumber).removeClass("d-none");

$.ajax({
    url: url,
    type: "GET",
    async: true,

    success: function (result) {
        $("#page_view"   pageNumber).html(result);
    }
});

In my main html file I just use a link to get the page and load it:

<a href="javascript: load_main_form('Account/Details');">DetailsPage</a>

In this method the problem is when I load and close one page multiple times Javascript of that page stores in cache by every load and some functions or events runs multiple times by every load.

I used ajaxComplete to get how many times it store it in cache.

How can I clear cache or rewrite on last one? Or even if there is a faster way to do this I will be thankful for sharing.

CodePudding user response:

  •  Tags:  
  • Related