Aspnetcore: Router intercepts clicks on <a> with target='_blank'

Created on 23 Aug 2018  路  9Comments  路  Source: dotnet/aspnetcore

Hello, blazor intercepts a click even if you specify target="_ blank" in the link.

Test Host:
http://domain.com/

Examples

<a href="/api/rewriteurl/test" target="_blank">not work</a>
<a href="https://domain.com/api/rewriteurl/test" target="_blank">WORK</a>
<a href="http://domain.com/api/rewriteurl/test" target="_blank">not work</a>
<a href="http://domain2.com/api/rewriteurl/test" target="_blank">WORK</a>

I think that it's not necessary to intercept links with target="_ blank"

Blazor Server side: 0.6.0-preview1-20180822.3
/_framework/blazor.server.js

area-blazor

Most helpful comment

@cores-system the only problem is that Blazor calls preventDefault when you click on a link so the page is not fully reloaded, look how I fixed the ctrl+click problem here : https://github.com/aspnet/Blazor/pull/868 it should be trivial to look for a target attribute.

On angular it's managed here on line 292 : https://github.com/angular/angular/blob/c8a1a14b87e5907458e8e87021e47f9796cb3257/modules/angular1_router/src/ng_route_shim.js

All 9 comments

Thanks for reporting this. If you are interested in submitting a PR to correct this (assuming it's simple and also comes with an E2E test) we'd be happy to consider it!

@SteveSandersonMS , Sent a solution

@cores-system: Hello, blazor intercepts a click even if you specify target="_ blank" in the link.

This is because Blazor is designed correctly.

@cores-system: I think that it's not necessary to intercept links with target="_ blank"

Blazor should not change how the web works.
Try your code anywhere, in any framework, and see that the click event is always intercepted. That's the way it was designed. Personally, I don't think Blazor should rectify this "flaw"

@enetstudio, Then another question, how to open a link in a new tab?
I just do not understand why to remove the ability to open a link in a new tab.

@cores-system the only problem is that Blazor calls preventDefault when you click on a link so the page is not fully reloaded, look how I fixed the ctrl+click problem here : https://github.com/aspnet/Blazor/pull/868 it should be trivial to look for a target attribute.

On angular it's managed here on line 292 : https://github.com/angular/angular/blob/c8a1a14b87e5907458e8e87021e47f9796cb3257/modules/angular1_router/src/ng_route_shim.js

@RemiBou, preventDefault () is not called if the link specified target="_blank"
https://github.com/aspnet/Blazor/pull/1354/commits/53d5da450fe856407c372f1ab76d10d687732093

I need to open the link in a new window by clicking "click", not just "ctrl+click"

@cores-system If you don't call preventDefault, the browser will handle the creation of the new window because it will see the _blank attribute. You could add E2E test for validating this

@RemiBou, I'll try to do E2E tomorrow.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danroth27 picture danroth27  路  130Comments

danroth27 picture danroth27  路  79Comments

reduckted picture reduckted  路  91Comments

mkArtakMSFT picture mkArtakMSFT  路  89Comments

radenkozec picture radenkozec  路  114Comments