21

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?

Code:

<%@ 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
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
user1054625
  • 461
  • 4
  • 7
  • 15

2 Answers2

35

This is largely taken from this MSDN article but window.external can be used to allow your WebBrowserControl to execute public methods of your client Windows Forms application.

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 WebBrowserControl you may have a button that looks like:

<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.

vpiTriumph
  • 3,116
  • 2
  • 27
  • 39
  • <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %>
    – user1054625 May 22 '12 at 00:49
  • Public Class WebForm1 Inherits System.Web.UI.Page Protected Sub SayHello(ByVal name As String) Response.Write("Hello :- " & name) End Sub End Class – user1054625 May 22 '12 at 00:50
  • 1
    Well 2 things. One is that you are probably better off modifying your original post and adding the code than forcing it into the comments, it makes it very hard to read :). Second is that your `SayHello` is `protected`. It must be `public`. Let me know if that fixes it. – vpiTriumph May 22 '12 at 00:54
  • I have edited the question. Now you have the complete code which i have. – user1054625 May 22 '12 at 01:01
  • Okay we are discussing entirely different things. You are dealing with a `System.Web.UI.Page` and what I sent you is for a `System.Windows.Form` what you are looking for is how to call server side code from the client side of a webform. Please check out this previous [Stack Overflow](http://stackoverflow.com/questions/5828803/how-to-call-code-behind-server-method-from-a-client-side-javascript-function) article, the post by @muhammad-akhtar midway down should solve your problem. – vpiTriumph May 22 '12 at 01:08
  • @vpiTriumph does *windows.external* use to refer public methods of the form which is hosting the WebControl or other Public Methods of Forms can be accessed too? – Volatil3 Apr 11 '13 at 08:10
  • 1
    @Volatil3 the Form (or application) hosting the browser component must set the scripting object to an instance of a com visible .net class.(see [WebBrowser.ObjectForScripting](https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.objectforscripting(v=vs.110).aspx)). The public methods of that class will be available using `window.external` – xr280xr Sep 22 '16 at 21:24
13

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.

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 )

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

Community
  • 1
  • 1
lzcd
  • 1,325
  • 6
  • 12