【问题标题】:Angular - Allow window.open to open new window and not a popup [duplicate]Angular - 允许 window.open 打开新窗口而不是弹出窗口[重复]
【发布时间】:2018-04-15 15:28:18
【问题描述】:

我正在尝试让window.open 在新标签页中打开。新标签基本上是一个 PDF 文件,会提示用户下载。

这是我的 Angular 4 代码:

download() {
    const data = {html: this.template.toArray()[0].nativeElement.innerHTML};
    this.authService.generateResume(this.template.toArray()[0].nativeElement.innerHTML).subscribe(res => {
      const path: string = res.success;
      const uri = path.substring(path.lastIndexOf('/'), path.length);
      window.open('http://example.com/pdf' + uri, '_blank');
    })
  }

我了解window.open 被视为弹出窗口。

我们怎样才能解决这个问题?

【问题讨论】:

  • 您无法控制它是在新窗口还是标签页中打开,这是用户在浏览器设置中设置的首选项
  • 反对票是怎么回事?!!用户点击Download按钮,下载PDF的功能被调用。有什么解决办法吗?
  • 我在之前的评论中解释了可以做什么或不可以做什么。你在说什么解决方法? ....顺便说一句,我没有投反对票
  • 这里的诀窍不是关于你的页面在哪里打开,而是关于它是否是作为用户交互的直接结果打开的。您应该将window.open 函数直接放在用户处理程序中(例如element.onclick = function(){window.open();})。如果您在该处理程序中放置另一个(子)函数并将您的window.open 放入该(子)函数中,浏览器会将其视为以编程方式打开的窗口,这是广告/恶意软件/其他任何内容的有力指标,并将为此阻止它。
  • 因此就如何实现这一点寻求帮助!我想不出办法process & generate the PDF 让用户同时下载 PDF。

标签: javascript angular window.open


【解决方案1】:

要打开一个新网页,请尝试使用:

位置。替换("//url");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多