0

I have created the hash load URL function in javascript, everything is perfect expected one issue, that is - I want, If I click on the URL <div class="loader"</div> will append. Is this basically a loader that will represent something loading. I already using this code $("body > div.content").append('<div class="loader"</div>'); but this loader only work when I reload the page. I want when I click on <a href="#page">CLICK HERE</a> types click the i will also show loader.

How I achieve the goal and fix my code. Please help me

function loadcontent(hash) {
  if (hash == "") {
    hash = "./home.html";
  }
  $("html body").animate({
    scrollTop: 0
  }, "600", "swing");
  $("body > div.content").load("directory/" + hash);
}

$(window).on("hashchange", function() {
  loadcontent(location.hash.slice(1));
});

var url = window.location.href;
var hash = url.substring(url.indexOf("#") + 1);
if (hash === url) {
  hash = "./home.html";
}
$("body > div.content").append('<div class="loader"</div>');
$("div.content > div").load("directory/" + hash, function(responseTxt, statusTxt, jqXHR) {
  if (statusTxt == "success") {
    $("div.loader").remove(); return true;
  } else {
    if (statusTxt == "error") {
      $("div.loader").remove();
      alert("Error : " + jqXHR.status + " " + jqXHR.statusText);
      return false;
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

1 Answer 1

1
<a href="#page" onclick="loadcontent(hash)">CLICK HERE</a>

You might have to return false in loadcontent() if you don't want to redirect.

Sign up to request clarification or add additional context in comments.

2 Comments

When I use onclick it not redirect the page, can you please update my script as you want to explain
return true below should solve it if (statusTxt == "success") { $("div.loader").remove(); return true; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.