-3

I'm trying to get window.open to open in a new tab. The new tab is basically a PDF file which would prompt the user to download.

Here's my Angular 4 Code:

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');
    })
  }

I understand window.open is treated as a popup.

How can we go around this?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Elaine Byene
  • 3,868
  • 12
  • 50
  • 96
  • You can't control if it opens in a new window or in a tab, that is a preference the user has set in the browser settings – Asons Apr 15 '18 at 15:35
  • What's with the downvote?!! The user is clicking on `Download` button and the function to download the PDF is thus called. Is there any work around for it? – Elaine Byene Apr 15 '18 at 15:39
  • I expained in my previous comment what can or cannot be done. What workaround are you talking about? .... and btw, I didn't downvote – Asons Apr 15 '18 at 15:41
  • The trick here is not about where your page is opened, but whether it's opened as a direct result of user interaction or not. You should put the `window.open` function directly inside the user-handler (e.g. `element.onclick = function(){window.open();}`). If you put another (sub)function inside that handler and put your `window.open` inside that (sub)function, the browser will see it as a programatically opened window, which is a strong indicator for ads/malware/whatever, and will therefor block it. – myfunkyside Apr 15 '18 at 15:45
  • And hence asking for help on how to get that going! I couldn't think of a way to `process & generate the PDF` and make the user download the PDF at the same time. – Elaine Byene Apr 15 '18 at 19:52
  • From an Angular question to Javascript question!! Thanks to whoever edited the question. It's useless now. – Elaine Byene Apr 15 '18 at 19:53
  • I think the Javascript tag makes more sense here, after all Angular is just a framework that works on top of Javascript, and the solution to your problem is more likely Javascript based than angular. Anyway, I think what you might be looking for is a hidden iFrame, and load the PDF into that. – Keith Apr 15 '18 at 20:13

1 Answers1

-3

To open a new web page, try using:

location. replace("//url");

M.A.Williams
  • 223
  • 1
  • 9