简体   繁体   English

Blazor 使用 JSRuntime 打开新选项卡/窗口

[英]Blazor open new tab/window using JSRuntime

I implemented opening a new tab/window from C# using the JSRuntime.我使用 JSRuntime 从 C# 实现了打开一个新选项卡/窗口。 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.它在桌面上运行良好,但在手机上使用 safari 时它没有任何作用:新选项卡无法在移动设备上打开。

Any one has had any luck getting this to work?有没有人有幸让这个工作?

await JSRuntime.InvokeAsync<object>("open", url, "_blank");

Add a javascript method to open the URL in another tab and call that method using JsRuntime.添加一个 javascript 方法以在另一个选项卡中打开 URL,并使用 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.如果您使用window.open() js 方法,它不适用于所有浏览器,因此需要动态创建a元素并在其上引发点击事件。

See below:见下文:

window.NavigateTo = (url) => {
  const link = document.createElement('a');
  link.href = url;
  link.target = '_blank';
  document.body.appendChild(link);
  link.click();
  link.remove();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM