1

I want to redirect to an external url after some action. Please refer my code

            this.http.get(this.apiurl+'api/insert.php?did='+this.did).subscribe(data=>{
                  var jsonData = data.json();    
                  let url = jsonData.urlpass;
// redirect to this url like https://flipkart.com with my affilate params
window.open(url ,'_blank'); 
    });

this window.open(url ,'_blank'); shows the popup blocker.

So I tried like this

<a #myDiv id="anchorID" href="url here" target="_blank"></a>
$("#anchorID")[0].click();

But this click event is not triggered inside the subscribe method.

If I use var newWin = window.open(); newWin.location = url_pass;

It creates the error as Cannot assign to 'location' because it is a constant or a read-only property.

I want to open this external url in new window with out popupblocker issue.

Please help anyone.

Rahul Kumar
  • 5,120
  • 5
  • 33
  • 44
Sam Hanson
  • 1,317
  • 4
  • 22
  • 48
  • Window open expects the url to be passed... and jQuery... have you tried angular tutorials? – smnbbrv Mar 20 '18 at 05:12
  • 1
    Exact duplicate of [Angular 2 - Redirect to an external URL and open in a new tab](https://stackoverflow.com/questions/42775017/angular-2-redirect-to-an-external-url-and-open-in-a-new-tab) – Black Mamba Mar 20 '18 at 05:30
  • Duplicate of https://stackoverflow.com/questions/34338440/how-to-redirect-to-an-external-url-in-angular2 – Bahman Mar 20 '18 at 05:36
  • is updated answer worked for you ?? – Pranay Rana Mar 21 '18 at 05:08

2 Answers2

0

can you try like this ,

component.html , add link to html in hidden mode

  <a #popupLink class="btn-excel" (click)='popup()' 
                                       style="display:none">
    <span>Dummy link</span>
  </a>

component.ts

  @ViewChild('popupLink') popupLink;

   popup() {
    window.open("https://www.google.com", "_blank");
  }

  //some where from code
  popupLink.click();
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • @SamHanson - cna you try solution now ?? – Pranay Rana Mar 20 '18 at 06:06
  • Yes. It shows error on popupLink.click(); line. Shall need to define popuplink . I am new to angular. Please give some directions – Sam Hanson Mar 20 '18 at 06:21
  • @SamHanson - updated just cause problem with variable name , i am trying to figure out if this resolves issue or not as it working for me, it good if you create link in html which is hidden – Pranay Rana Mar 20 '18 at 07:07
0

Try this

var newWin = win.open('some_url');
Akanksha Gaur
  • 2,636
  • 3
  • 26
  • 50