1

I want to show a popup, when user click on a link that is external to angular application.

How do I check whether the url is external? E.g. pointing to the same domain, but different document

<a [href]="someUrl" appCheckExternalUrl>{{someUrl}}</a>
@Directive({
  selector: '[appCheckExternalUrl]'
})
export class CheckExternalUrlDirective {

  constructor(private el: ElementRef<HTMLAnchorElement>) { }

  get url() { return this.el.nativeElement.href; }

  get isExternalUrl() {
    return ???;
  }
}

The tricky part is that it depends on router settings.

Let's say my app is hosted in mydomain.com/myapp and defines route /myroute. Then:

  • mydomain.com/myapp/myroute - internal route
  • mydomain.com/anotherapp - external route.

But if the router uses hashtag routing, then

  • mydomain.com/myapp/myroute is external.

Also this url is also external, because it is not handled by application:

  • mydomain.com/myapp/assets/logo.png
Liero
  • 25,216
  • 29
  • 151
  • 297
  • You can pull the current link via flat js or using Angular's router: https://stackoverflow.com/a/45185477/7733570. It then becomes a simple js question on how to test if it's external: https://stackoverflow.com/a/2911045/7733570 – Z. Bagley May 29 '19 at 14:44
  • Possible duplicate of [How to get current route](https://stackoverflow.com/questions/34597835/how-to-get-current-route) – Z. Bagley May 29 '19 at 14:45
  • @Z.Bagley: how that could possibly be duplicate?? – Liero May 29 '19 at 19:27
  • With the changes, it makes this a very different question, but this can likely be broken down into a few other questions. 1) How do I get all my internal angular routes? 2) How do I check what is the current route? (answer shown above) 3) Regex to check if 1 matches 2? (answer shown in first comment again). 1) is answered here: https://stackoverflow.com/a/46150355/7733570 Hope that helps. – Z. Bagley May 30 '19 at 19:46

1 Answers1

-1

You can use window.origin to get the url of the page you're in, so your angular Page.

So you just have to compare the origin of your site with the origin of the href.

Scieur Arnaud
  • 823
  • 2
  • 7
  • 13
  • this is not correct answer. It only says whether the origin is the same, but my question is completely different. – Liero May 29 '19 at 19:26