0

I am making a webpage in which I have a anchor tag '' having a URL in href but the problem is that on clicking on this anchor tag it will check the condition through ajax and by the response from the ajax it will decide whether to send to another page or not.

I have tried this so far the ajax is working good but when I click on the anchor tag the new page is open in new window and ajax run on the same and it doesn't matter whether the response is true or false

<a href="www.recordlist.com" class = "checkrecord" check_us_type = "user" target = "_blank"> check</a>      


<script>
 $(".checkrecord").click(function(e){
        e.preventDefault();
        var user_type = $(this).attr("check_us_type"); 
        $.ajax({
            type: "POST",
            data: { 'ven_type' : ven_type },
            url: baseUrl+"/CheckUser",
            success: function(result){
              if(result == "-1")
              {
                  alert("Please login to view Details");
              }else

              {return true;// here it should work but it doesn't

              }
            },
        });
    });

</script>
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
bhardwaj_g
  • 35
  • 9

2 Answers2

0

You've to redirect it manually, when you've prevented the default behavior of a tag like this

<script>
  $(".checkrecord").click(function(e) {
    e.preventDefault();
    var ele = e.target;
    var user_type = $(this).attr("check_us_type");
    $.ajax({
      type: "POST",
      data: {
        'ven_type': ven_type
      },
      url: baseUrl + "/CheckUser",
      success: function(result) {
        if (result == "-1") {
          alert("Please login to view Details");
        } else {
          window.location.href = $(ele).attr("href");
        }
      },
    });
  });
</script>
Shridhar Sharma
  • 2,337
  • 1
  • 9
  • 13
0

The e.preventDefault(); should prevent the new window to open. Is it loaded? Did you run the code on document ready? If you just run the code on JS load you are probably experiencing race conditions and the HTML element is not there yet when the code executes.

$( document).ready(function() {
   $(".checkrecord").click(function(e){
        e.preventDefault();
        var user_type = $(this).attr("check_us_type"); 
        $.ajax({
            type: "POST",
            data: { 'ven_type' : ven_type },
            url: baseUrl + "/CheckUser",
            success: function(result){
              if(result == "-1")
              {
                  alert("Not logged in!");
              }else

              {
              alert("Logged in!");
              }
            },
            error: function(result){
                alert("error!");
            },
        });
    });

  });

I have also added a second callback (error), which is a much better way of handling results. Have the CheckUser script return with HTTP codes 200 for OK and 403 for denied/forbidden. This is how rest works and what jQuery understands, no need for custom checks.

Daan
  • 191
  • 10
  • it will redirect to another page after the condition is true that is why i have have disabled by using e.preventDefault – bhardwaj_g May 06 '19 at 12:05
  • It *_should_* redirect to another page on true. But the fact that there is no code in your example that makes that happen makes me wonder if the code is loaded correctly at all. You should also implement something like this: https://stackoverflow.com/questions/19851782/how-to-open-a-url-in-a-new-tab-using-javascript-or-jquery after you make the condition work. Keep it simple, start with alerts (or console logs) – Daan May 06 '19 at 12:11