Digi Jeff Digi Jeff - 3 months ago 18
jQuery Question

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

How can I have all links with a

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
folder if that helps.


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');