Digi Jeff Digi Jeff - 1 year ago 77
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.

Answer Source

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');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download