I'm trying to open a link in a new tab from a Blazor Server application. I tried several methods such as
await JSRuntime.InvokeVoidAsync("open", action.ActionData, "_blank");
as well as
await JSRuntime.InvokeVoidAsync("NavigateTo", action.ActionData);
whereas the NavigateTo function appears as follows:
window.NavigateTo = (url) => {
const link = document.createElement('a');
link.href = url;
link.target = '_blank';
document.body.appendChild(link);
link.click();
link.remove();
}
Everything is working fine in all browsers except for Safari on IOS.
Has anybody found a solution to this problem? As I found out, there is a problem opening a new tab in Safari from an asynchronous task. But I couldn't find a way to start this synchronously from Blazor Server.
I found a quite simple answer to my problem. As the tab stays open, I could just execute the action in a onclick event and add a href attribute to the link with the target="_blank" attribute.
The browser will open the link in a new tab and execute the action in the background in the old tab.