Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use an empty href attribute without redirect?

Is there a way to have href without being redirect back to the root in angular2? As I have this <a href="" (click)="modal.open()"></a> code to open up a modal but its redirecting me back to my home page.

I know its because of the routeConfig angular has, but is there a way to say bypass that?

{ path: '', component: LoginComponent, pathMatch: 'full'},

Note sure if removing href completely is valid, it may be without check w3c validation.

like image 792
nCore Avatar asked Aug 04 '16 08:08

nCore


People also ask

How do I use an empty href?

From the URL List, click the blue Hint Details button, which will bring up a slide-across window with specific details about the Hint for each URL. This will show you any outgoing anchor links with empty href attributes, highlighted in red in the HTML.

Can I leave href blank?

Indeed, you can leave it empty (W3 validator doesn't complain). Taking the idea one step further: leave out the ="". The advantage of this is that the link isn't treated as an anchor to the current page.

How do I create a blank link in HTML?

a target=”_blank” Open in New Browser Tab (or Window) The target attribute specifies where the linked document will open when the link is clicked. The default is the current window. If target="_blank" , the linked document will open in a new tab or (on older browsers) a new window.

How do I create a blank link?

You can use the target="_blank" attribute if you want your users to click on a link that opens up a new browser tab. The target="_blank" attribute is used inside the opening anchor tag like this.


2 Answers

I think a better answer to this question could be:

<a href="" (click)='$event.preventDefault();modal.open()'>
like image 138
Erlindo Avatar answered Oct 11 '22 06:10

Erlindo


href="javascript:;" will produce an error after clicking link when Content-Security-Policy disallows inline scripts (script-src: 'self'; for example) (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src).

You can use empty href with event.preventDefault() on click instead.

<a href="" (click)="$event.preventDefault()">Link</a>
like image 42
an-angular-dev Avatar answered Oct 11 '22 06:10

an-angular-dev