Jump to content

Open ALL pdfs in new tab instead of downloading

Recommended Posts

Site URL: https://www.robinburgess.com/

On this website, some of the pdfs open in new tabs, while some are being downloaded. This seems inconsistent to me, because I link all pdfs in markdown with 

<a href="/s/link-to-.pdf" target="_blank">Linktext</a>

However, the behavior of the pdfs changes, seemingly randomly. For example, when clicking on "DEMAND FOR ELECTRICITY ON THE GLOBAL ELECTRIFICATION FRONTIER", the pdf opens in a new tab, but for "WHY DO PEOPLE STAY POOR?", it downloads. I'm using Google Chrome, but I want the behavior to be the same across all modern browsers.

I have already tried to force this with a site-wide code injection, but it didn't work.

<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>

 

Link to comment
  • 2 years later...
  • Replies 2
  • Views 5.5k
  • Created
  • Last Reply

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.