C# window.external 有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10694341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:51:05  来源:igfitidea点击:

What is the use of window.external?

c#javascriptasp.netvb.net

提问by user1054625

What is the use of window.external ? Is this used to call the server side functions / methods in C# / VB.NET (ASP.NET) from JavaScript ? Can you please point me in right direction ?

window.external 有什么用?这是否用于从 JavaScript 调用 C#/VB.NET (ASP.NET) 中的服务器端函数/方法?你能指出我正确的方向吗?

Thanks

谢谢

Error:

错误:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="button" name="button1" value="Click" onclick="javascript:window.external.SayHello('Mike');" />
    </div>
    </form>
</body>
</html>

Public Class WebForm1
    Inherits System.Web.UI.Page

    Public Sub SayHello(ByVal name As String)
        Response.Write("Hello :- " & name)
    End Sub

End Class

采纳答案by vpiTriumph

This is largely taken from this MSDN articlebut window.externalcan be used to allow your WebBrowserControlto execute public methods of your client Windows Forms application.

这主要取自这篇MSDN 文章,window.external可用于允许您WebBrowserControl执行客户端 Windows 窗体应用程序的公共方法。

For example in your form you may have a function such as:

例如,在您的表单中,您可能具有以下功能:

public void HelloFromTheForm()
{
    MessageBox.Show("Hi client, thanks for calling me!");
}

And in the html loaded into your WebBrowserControlyou may have a button that looks like:

在加载到您的 html 中,WebBrowserControl您可能有一个如下所示的按钮:

<button onclick="window.external.HelloFromTheForm()">
    Say hi to the form
</button>

So in regards to your question of 'Is this used to call the server side functions?', your form isn't 'server side' but it does allow you to call the C#/VB.NET code of your form from an embedded webpage.

因此,关于“这是否用于调用服务器端函数?”的问题,您的表单不是“服务器端”,但它确实允许您从嵌入式网页调用表单的 C#/VB.NET 代码.

回答by lzcd

It is a convention utilised by some of the browser / operating system vendors to facilitate communication between javascript running within the browser and code running "outside" of the browser on the users device or machine.

这是一些浏览器/操作系统供应商用来促进在浏览器内运行的 javascript 与在用户设备或机器上的浏览器“外部”运行的代码之间进行通信的约定。

For example, if you've written a native application for Android or Windows Phone that hosts a web browser control, the surrounding native mobile framework might provide window.external as a way for javascript running on the web page within the web control to call out to your app's native code functionality. (An example of how to such things for Android can be found here: Listen to javascript function invocation from java - Android)

例如,如果您为托管 Web 浏览器控件的 Android 或 Windows Phone 编写了本机应用程序,则周围的本机移动框架可能会提供 window.external 作为在 Web 控件内的网页上运行的 javascript 进行调用的一种方式到您的应用程序的本机代码功能。(可以在此处找到有关如何为 Android 执行此类操作的示例:Listen to javascript function invocation from java - Android

If, on the other hand, you're looking to communicate between the javascript running on the user's web browser and the C# code running on your server then you'll be wanting to investigate AJAX style calls (which usually has very little to do with window.external). Examples of set up such things can be found at the ASP.Net site. e.g. http://www.asp.net/web-forms/tutorials/aspnet-ajax/understanding-asp-net-ajax-web-services

另一方面,如果您希望在用户 Web 浏览器上运行的 javascript 和服务器上运行的 C# 代码之间进行通信,那么您将想要研究 AJAX 样式调用(这通常与窗口。外部)。可以在 ASP.Net 站点上找到设置此类内容的示例。例如http://www.asp.net/web-forms/tutorials/aspnet-ajax/understanding-asp-net-ajax-web-services