-2

My HTML:

 <a id="link" class="linkRedirect" data-href="">Download Link</a>

I am getting incorrect value for $("#link").data("href") when clicked multiple times.

Click Handler:

 $(document).on("click", "#link", function (e) {
            var url = $("#link").data("href");
            //check validation
            if(true) location.href = url;
 });

The file in the url will be downloaded only if the session is valid.

Joseph
  • 91
  • 1
  • 7

3 Answers3

0

You could rather use it like this:

<a id="link" class="linkRedirect" rel="somelink">Download Link</a>

then ins your jquery, use this:

 $(document).on("click", "#link", function (e) {
            var url = $("#link").attr("rel");
            //check validation
            if(url)//apply whatever check you want
             location.href = url;
 });
Sayed
  • 601
  • 6
  • 21
0

Your jQuery code is really weird. Anyway check with that:

$('#link').click(function(){
  var url = $("#link").attr("data-href"); 
  if(url){
    window.location = url;
  }
});

I didn't test it but that would work better than your code :)

enguerranws
  • 8,087
  • 8
  • 49
  • 97
0

Got the problem. When I renamed attribute data-href to data-url, the issue was solved.

$("#link").data("href")

The value obtained was that of href attribute and not data-href.

Joseph
  • 91
  • 1
  • 7