I am working on a web app, which, when added to HomeScreen, will act as a standalone app, meaning there are no browser UI available.
At one point I need to open a file, the URL to which will be generated only when the link has been clicked. Here is the template:
<a class="mobile-target"
(click)="download($event, doc)"
[id]="doc.dokumentGuid"
[title]="doc.name"><span>{{dokumentMime(doc)}}</span></a>
Here is the method which handles the click in the component:
download($event, dokument: Dokument) {
$event.preventDefault();
this.downloading = true;
dokument.isNew = false;
if (isMobile()) {
const anchor = this.document.getElementById(dokument.dokumentGuid);
this.kundeService
.getDokumentDownloadUrl(dokument.dokumentGuid)
.pipe(
tap(url => this.setAndClick(anchor, url)),
finalize(() => (this.downloading = false))
)
.subscribe();
} else {
this.kundeService
.getDokumentData(dokument.dokumentGuid)
.pipe(
tap(blob => saveBlobAs(blob, dokument.name)),
finalize(() => (this.downloading = false))
)
.subscribe();
}
}
setAndClick(anchor, url) {
anchor.setAttribute('href', url);
anchor.setAttribute('target', '_blank');
// see: https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/compatibility/dn905219(v=vs.85)
const event =
typeof (<any>window).Event === 'function'
? new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true
})
: document
.createEvent('MouseEvents')
.initMouseEvent(
'click',
true,
true,
window,
0,
0,
0,
0,
0,
false,
false,
false,
false,
0,
null
);
anchor.dispatchEvent(event);
}
Some versions of iOS do open a Safari app and a new window in it. The latest iOS12 on an iPhone 7S (and I have no idea why iPhone 6 is ok with it), will open the link in the same standalone window, thus making it impossible to go back to the page where the link was clicked (as there is no UI in the standalone mode).
Why would Safari sometimes ignore the target=_blank and will not open a new Safari window?