Home > Software engineering >  Opening PDF's using `target="blank"` fails on Android Chrome
Opening PDF's using `target="blank"` fails on Android Chrome

Time:12-24

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.

CodePudding user response:

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

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

CodePudding user response:

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

  • Related