Chris Happy
Chris Happy

Reputation: 7295

Opening PDF's using `target="blank"` fails on Android Chrome

I have a bunch of PDF links that open in new tabs. On Desktop, this works perfectly.

However, when using a mobile phone (only tested on Samsung Galaxy's), the PDF links just open a new tab for a split second, then close without downloading the file.

<a href="http://www.africau.edu/images/default/sample.pdf" target="_blank">
  Open PDF
</a> 
(this just quickly opens and closes a new tab when using Chrome on my Samsung Android)

JSfiddle | CodePen

How can I allow users to open files in a new tab on Desktop and just download them on Mobile? (Preferably, only using HTML)

Note: removing target="_blank" does not solve the issue.

Upvotes: 1

Views: 3874

Answers (3)

Lorenzo
Lorenzo

Reputation: 1049

I had the same issue and was wondering why the selected answer was working. The point is not the relative/absolute URL, but the fact that you are trying to open a static resource on HTTP starting from a website that is probably under HTTPS . Using relative urls magically do just the same.

I solved by corrected the links forcing HTTPS on the PDF I needed to open.

Upvotes: 1

Chris Happy
Chris Happy

Reputation: 7295

Make the URL relative to the site domain

<a href="/sample.pdf" target="_blank">Open PDF</a> 
<!-- Not <a href="/example.com/sample.pdf>Open PDF</a> -->

This allowed the links to download without issue on the Google Chrome app (specifically on Samsung).

Upvotes: 2

An Nguyen
An Nguyen

Reputation: 321

You should better display pdf in mobile browser via Google Doc Viewer:

http://docs.google.com/gview?embedded=true&url=<url of your doc>

Upvotes: 2

Related Questions