-1

I want to know if this is possible to do via jquery since I CAN'T add it in the markup:

I want to add download.php?file= to a url like this (links will change so I can't just replace the whole href):

<a class="boton" target="_blank" href="http://www.test.com/wp-content/uploads/2014/11/PRUEBA-ES.pdf">DOWNLOAD</a>

So I could get the link like this:

<a class="boton" target="_blank" href="http://www.test.com/wp-content/uploads/2014/11/download.php?file=PRUEBA-ES.pdf">DOWNLOAD</a>

Any ideas?

codek
  • 343
  • 4
  • 20
  • 49
  • You are getting downvotes because there are several answers to this question available with trivial searching. Please be sure to research questions before asking them in the future -- this helps us keep the site organized and useful. – Chris Baker Nov 14 '14 at 01:02

2 Answers2

2

This should work

$('.boton').each(function (i) {
    var hrefOrig = $(this).attr('href');
    var segments = hrefOrig.split('/');
    var file     = segments.pop()
    var hrefNew  = hrefOrig.replace(file, 'download.php?file='+file);

    $(this).attr('href', hrefNew)
})

@ariel_556 answer will likely work, but this one matches exactly how you described the URL

elzi
  • 5,442
  • 7
  • 37
  • 61
-1

yea. i think it would be better if you implement jQuery to make it more understandable

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
    var url = $('.boton').attr('href'),
        arr = url.split('/'),
        file = arr.pop();

    $('.boton').attr('href', arr.join('/') + '/download.php?' + file);
</script>
ariel_556
  • 368
  • 1
  • 9
  • This gives me the following output: `href="http://www.test.com/wp-content/uploads/2014/11/PRUEBA-ES.pdfdownload.php?file=PRUEBA-ES.pdf"` It's repeating the file – codek Nov 14 '14 at 00:45