Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Automatically open all pdf links in new tab

Posted on 2016-12-01
2
Medium Priority
?
333 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
While Plesk offers many potential benefits to website administrators, including compatibility with Windows Server and other leading technologies, the company has also been working to differentiate it from other control panels for content management…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
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…
Suggested Courses

916 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