Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blazor open new tab/window using JSRuntime

Tags:

c#

safari

blazor

I implemented opening a new tab/window from C# using the JSRuntime. It works fine on the desktop, but when using safari on the phone it doesn't do anything: the new tab fails to open on mobile devices.

Any one has had any luck getting this to work?

await JSRuntime.InvokeAsync<object>("open", url, "_blank");
like image 736
Ricardo Chaidez Avatar asked Sep 14 '25 16:09

Ricardo Chaidez


1 Answers

Add a javascript method to open the URL in another tab and call that method using JsRuntime.

If you use window.open() js method it wont work on all browsers so need to dynamically create an a element and raise a click event on it.

See below:

window.NavigateTo = (url) => {
  const link = document.createElement('a');
  link.href = url;
  link.target = '_blank';
  document.body.appendChild(link);
  link.click();
  link.remove();
}
like image 181
Umair Avatar answered Sep 17 '25 05:09

Umair