Heather Ritchey
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:
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER