Digi Jeff Digi Jeff - 16 days ago 5
jQuery Question

Make any link with .pdf open in new window with jQuery?

How can I have all links with a

.pdf
file extension open in a new window using jQuery? I need to change this:

<a href="domain.com/pdf/parkingmap.pdf">parking map</a>


In to this:

<a href="domain.com/pdf/parkingmap.pdf" target="_blank">parking map</a>


All files are in a
/pdf
folder if that helps.

Answer

To achieve this you can select any a element which has a href property ending with .pdf, and add a target="_blank" attribute to it. Try this:

$(function() {
    $('a[href$=".pdf"]').prop('target', '_blank');
});