Link to home
Start Free TrialLog in
Avatar of Heather Ritchey
Heather RitcheyFlag for United States of America

asked on

Automatically open all pdf links in new tab

Does anyone know of a working script that will make all pdf links in a site open to a new tab? After adding a lot of pdf links to a site in development, the owners changed their minds and now want them to open in a new tab instead of the same window.

I'm hoping for a script to do it rather than having to comb through the entire site and edit all the links. I tried this, but it doesn't work - no errors showing from it, just isn't making them open in a new tab as expected:
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> 
<script>
 $('a[href*="pdf"]').click(function(e) {
     e.preventDefault(); // stop the existing link from firing
     var documentUrl = $(this).attr("href"); // get the url of the pdf
     window.open(documentUrl, '_blank'); // open the pdf in a new window/tab
   });
 </script>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Siva Prasanna Kumar
Siva Prasanna Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Heather Ritchey

ASKER

That worked perfectly. Thank you very much!! That saved a lot of time and extra work.