0

I am using ajax to redirect pages without refreshing for a tags. However, it randomly works. Sometimes, it doesnt even generate log that I put inside the function on purpose to check if its called. I guess a tag performs href that is already declared on inline. How do I make this 100% work?

$(".view-about-us a").on('click', event=>{

event.preventDefault();
var new_url = $(this).attr("href");

$.ajax({
    dataType: 'html',
    type: "POST",
    url: new_url,
    data: {

    },
    success: function(result) {
      window.history.pushState(null, null, new_url);

      var htmlString = result;

      var parser = new DOMParser();
      var doc = parser.parseFromString(htmlString, "text/html");
      var parent = doc.querySelector(".main-container");
      var children = parent.childNodes;

      children.forEach(child => console.log(child));

      $(".main-container").html(children);
    },
    error: function(result) {
        alert('error');
    }
});
});
Hclee
  • 43
  • 2
  • 8

1 Answers1

0

One thing you could do is make all of you href attributes javascript:void(0), then store what had previously been your href in a data attribute, such as data-href.

kshetline
  • 12,547
  • 4
  • 37
  • 73
  • I cant do that, because I am working under Drupal and i guess Drupal doesnt allow me to do that. Btw, I checked new_url, but it returns undefined. So, I guess i need to find a way to make it return proper url. – Hclee Jul 18 '18 at 03:37