有一次我需要打开一个文件,只有当链接被点击时才会生成指向它的URL。模板如下:
<a class="mobile-target"
(click)="download($event, doc)"
[id]="doc.dokumentGuid"
[title]="doc.name"><span>{{dokumentMime(doc)}}</span></a>
以下是处理组件中单击的方法:
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);
}
iOS版本确实会打开一个Safari应用程序,并在其中打开一个新窗口。iphone7s上的最新iOS12(我也不知道iphone6为什么可以使用它)将以同样的方式打开链接
独立的
为什么Safari有时会忽略target=\u blank而不打开新的Safari窗口?