2

How to open a pdf in iOS 10.3.3 safari browser in a new tab using javascript. The html element is hidden on which the virtual click will happen.

<a id="obv-download" target="_blank" download="download" href=""></a>

Using Ajax href field is getting filled and I need the same anchor link to open in new tab.The code which I am using for opening the pdf is below (working fine till iOS 10.2).

$('#id_of_elem').attr('href', url);
var click_ev = document.createEvent("MouseEvents");
click_ev.initEvent("click", true, true);
document.getElementById("id_of_elem").dispatchEvent(click_ev);

I already tried

$("#id_of_elem").click();

and

window.location.href('link');

But they did not workout.

Please help me

Inderjeet Singh
  • 556
  • 1
  • 9
  • 22

1 Answers1

0

It is impossible to open a link in a new tab or new window by yourself without a user click, browsers block popup abuse. You can either wait for the user to click "Download the PDF" (Use something like this: <a href='/doc.pdf' download target='_blank'>Download the PDF</a>) or you can remove the "target=_blank" and then the popup blocker will not block the download (this will result in navigation away from your page, however.)

Robert Moore
  • 2,207
  • 4
  • 22
  • 41
  • Thanks for the answer. The problem I am facing is that the link to download pdf is not available at the time of page load, when a user clicks on "get pdf" a unique link came through an Ajax call. So I need that same link to open in new tab in iPhone/iPad, otherwise its working on every other platform whatsoever. – Inderjeet Singh Aug 27 '17 at 05:10
  • @Goldy I'm not sure exactly what you're saying, but it sounds like you might not be wrapping your JS code in `addEventListener('load', function() { /*your code here*/ });`? – Robert Moore Aug 27 '17 at 13:04