Angular2 PWA / Safari 无法在新 window 中打开 link
Angular2 PWA / Safari can't open a link in new window
我正在开发一个 Web 应用程序,当添加到 HomeScreen 时,它将作为一个独立的应用程序运行,这意味着没有可用的浏览器 UI。
有一次我需要打开一个文件,URL 只有在 link 被点击时才会生成。这是模板:
<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 应用程序并在其中打开一个新的 window。 iPhone 7S 上的最新 iOS12(我不知道为什么 iPhone 6 可以),将在同一个 [=24= 中打开 link ]standalone window,因此无法返回到单击 link 的页面(因为在独立模式下没有 UI)。
为什么 Safari 有时会忽略 target=_blank 并且不会打开新的 Safari window?
我不能说为什么 iPhone6 和 iPhone7s 在浏览器行为方面存在差异。但是在我的测试中清楚地发现,同一主机的所有 links,在 stand-alone 模式下,也在相同的 window 中打开。 link 是用 javascript 生成的还是硬编码的 link 并不重要。对我有帮助的是引入了一个用于下载的子域 ("download.yourDomain ....")。
重要的是下载范围 link。
在您的 PWA 中,html header 中的基本 href 定义了范围。
有关范围主题,请参见此处 https://developer.mozilla.org/en-US/docs/Web/Manifest
据我所知,Apple 忽略了清单和范围。
我正在开发一个 Web 应用程序,当添加到 HomeScreen 时,它将作为一个独立的应用程序运行,这意味着没有可用的浏览器 UI。
有一次我需要打开一个文件,URL 只有在 link 被点击时才会生成。这是模板:
<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 应用程序并在其中打开一个新的 window。 iPhone 7S 上的最新 iOS12(我不知道为什么 iPhone 6 可以),将在同一个 [=24= 中打开 link ]standalone window,因此无法返回到单击 link 的页面(因为在独立模式下没有 UI)。
为什么 Safari 有时会忽略 target=_blank 并且不会打开新的 Safari window?
我不能说为什么 iPhone6 和 iPhone7s 在浏览器行为方面存在差异。但是在我的测试中清楚地发现,同一主机的所有 links,在 stand-alone 模式下,也在相同的 window 中打开。 link 是用 javascript 生成的还是硬编码的 link 并不重要。对我有帮助的是引入了一个用于下载的子域 ("download.yourDomain ....")。 重要的是下载范围 link。 在您的 PWA 中,html header 中的基本 href 定义了范围。
有关范围主题,请参见此处 https://developer.mozilla.org/en-US/docs/Web/Manifest
据我所知,Apple 忽略了清单和范围。