JavaScript function callback

Source: Internet
Author: User

JavaScript function callback

We often use the client and Web project in combination with the development requirements, this will involve executing the foreground dynamic Script Function on the client, that is, the function callback. This article provides an example to illustrate the callback function process.

First, a Web project is created. A simple page has only one button and textbox. The code and effect are as follows:

<%@ Page Language=C# AutoEventWireup=true CodeBehind=Default.aspx.cs Inherits=WebTest.Default %>
<Script language = javascript type = text/javascript src = EasyUI/jquery-1.8.0.min.js> </script> <script language = javascript type = text/javascript> $ (document ). ready (function () {$ (# btnTest ). bind (click, function () {window. external. checkUser () ;}); function CallBackFun (e) {$ (# txtValue ). val (e) ;}</script>


CheckUser: A method of the client.

CallBackFun: the callback function of the client.

In the btnTest Click Event, execute the client's CheckUser method. When the client calls back the CallBackFun function, assign a value to the text box with the ID txtValue. The value is the content returned from the callback function.

 

The following code creates a client program and the interface is very simple. There is only one webBrowser1 button. The code and interface are as follows:

 

Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using System. security. permissions; namespace WindowsFormsApplication1 {[PermissionSet (SecurityAction. demand, Name = FullTrust)] [System. runtime. interopServices. comVisibleAttribute (true)] public partial class Form1: Form {public Form1 () {InitializeComponent ();} public WebBrowser Browser {get {return this. webBrowser1 ;}} private void Form1_Load (object sender, EventArgs e) {webBrowser1.ObjectForScripting = this; webBrowser1.Navigate (http: // localhost: 8008/default. aspx);} public void CheckUser () {WindowsFormsApplication1.Comm. global. main = this; bool result = true;/** any logic can be implemented between them. You can assign the result value to the result ***/if (result) // judgment result {WindowsFormsApplication1.Comm. global. main. invoke (new MethodInvoker (delegate () {WindowsFormsApplication1.Comm. global. main. browser. document. invokeScript (CallBackFun, new object [] {777 });}));}}}}


The URL of webBrowser1 (http: // localhost: 8008/default. aspx) is the IIS Site on my local computer.

The callback function CallBackFun is implemented through the InvokeScript method of webBrowser1. An object is returned, and the "777" string is returned here.

After running, click the button. The effect is as follows:

It can be seen that the client has successfully called back the CallBackFun function and has passed the return value "777" of the client to the foreground.

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.