javascript - Angular2 PWA/Safari 无法在新窗口中打开链接

标签 javascript angular safari mobile-safari progressive-web-apps

我正在开发一个网络应用程序,当添加到 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://learn.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 窗口?

最佳答案

我不能说为什么 iPhone6 和 iPhone7s 在浏览器行为方面存在差异。但在我的测试中清楚地发现,在单机模式下,指向同一主机的所有链接也在同一窗口中打开。链接是使用 javascript 还是硬编码链接生成的并不重要。对我有帮助的是引入了一个用于下载的子域(“download.yourDomain ....”)。 重要的是下载链接的范围。 在您的 PWA 中,html header 中的基本 href 定义了范围。

有关范围主题,请参见此处 https://developer.mozilla.org/en-US/docs/Web/Manifest

据我所知,Apple 忽略了 list 和范围。

关于javascript - Angular2 PWA/Safari 无法在新窗口中打开链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52408667/

相关文章:

javascript - 旋转文本容器自动高度

Angular AuthGuard 不工作

javascript - 映射函数之后可能的 reducer 情况。

javascript - javascript中的日期时间计算库

javascript - Underscore 模板未使用 Backbone.js 渲染数据未传递到模板

css - SassError : @use rules must be written before any other rules. Angular

angular - 如何使用 Jasmine 在 Angular 单元测试中模拟 window.screen.width

google-chrome - 如何使用网络音频 api 无缝循环声音

CSS 转换在 Safari 中不起作用?有人知道解决方法吗?

javascript - iOS Safari : 'click' event not triggerd when there is an event-listener on 'mousemove'