Ricardo Chaidez
Ricardo Chaidez

Reputation: 41

Blazor open new tab/window using JSRuntime

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");

Upvotes: 4

Views: 4008

Answers (1)

Umair
Umair

Reputation: 5481

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();
}

Upvotes: 2

Related Questions