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
The external property of the Window API returns an instance of the External interface, which was intended to contain functions related to adding external search providers to the browser. However, this is now deprecated, and the contained methods are now dummy functions that do nothing as per spec.
The window object represents an open window in a browser. If a document contain frames (<iframe> tags), the browser creates one window object for the HTML document, and one additional window object for each frame.
A global variable, window , representing the window in which the script is running, is exposed to JavaScript code. The Window interface is home to a variety of functions, namespaces, objects, and constructors which are not necessarily directly associated with the concept of a user interface window.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With