【发布时间】:2018-09-19 14:50:52
【问题描述】:
我正在开发一个网络应用程序,当它添加到 HomeScreen 时,它将充当独立应用程序,这意味着没有可用的浏览器 UI。
有一次我需要打开一个文件,只有在单击链接时才会生成该文件的 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 应用程序和一个新窗口。 iPhone 7S 上的最新 iOS12(我不知道为什么 iPhone 6 可以使用它),将在同一个 standalone 窗口中打开链接,因此无法返回到点击链接(因为在独立模式下没有 UI)。
为什么 Safari 有时会忽略 target=_blank 而不会打开新的 Safari 窗口?
【问题讨论】:
标签: javascript angular safari mobile-safari progressive-web-apps