Solved

Automatically open all pdf links in new tab

Posted on 2016-12-01
2
74 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …

820 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