Solved

Automatically open all pdf links in new tab

Posted on 2016-12-01
2
54 Views
Last Modified: 2016-12-01
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

0
Comment
Question by:Dzynit
2 Comments
 
LVL 23

Accepted Solution

by:
Siva Prasanna Kumar earned 500 total points
ID: 41909182
I think you can use a function link this, which makes all href with .pdf to open in new blank page.

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

Author Closing Comment

by:Dzynit
ID: 41909211
That worked perfectly. Thank you very much!! That saved a lot of time and extra work.
1

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now