VB.NET/C# and JavaScript communication
Introduction
This article is about passing data between VB.NET/C# WinForms and JavaScript.
Before reading further, let me warn you that this article is not about ASP.NET. Concepts covered in the article are applied to Desktop applications.
Background
I was working on a project which required data transfer between my VB.NET WinForms application and the JavaScript (inside an HTML page). Along the way, I hit certain problems, and trying to resolve them cost plenty of time on the web (on Google mostly), so I thought of this article as a platform for developers looking to sort out similar issues. There isn't much detail on this topic on the web apart from a couple of Hello World examples from Microsoft on MSDN.
Starting point
OK, without any further talk, I will dig in to the subject.
Hello World
To start off, we'll start with a very simple example; all this will do is call a JavaScript function from VB.NET to display an alert with message 'Hello world'. Similarly, from the HTML page using JavaScript, we'll call a VB.NET function which will again display a messagebox with the message 'Hello world'. These are the steps you need to do to make it happen:
Calling JavaScript from VB.NET
In Visual Studio, create a new WinForms application, name it anything you like.
Add an import statement like Imports System.Security.Permissions in your form1 (main form).
Add a couple of attributes to form1, like:
Collapse Copy Code
<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
Public Class Form1
End Class
All they do is tell the .NET Framework that we want fulltrust and make the class visible to COM so this class is visible to JavaScript.
Add a WebBrowser control to the form and set its url property to c:\temp\mypage.html (just an example path, you should set it to the HTML page you'll be using).
Add a Button control on the form, and on its click handler, write this code:
Collapse
相关文档:
JavaScript中的JSON
JavaScript是为网景浏览器做页面脚本语言而实现的一种编程语言。它现在还被很多人误解是java的子集。它是一种具有类C语法和弱对象的模式语言。JavaScript完全遵守ECMAScript语言说明书第三版。
JSON是JavaScript对象文字记号的子集。由于JSON是JavaSript的子集,所以在JavaScript里, ......
通过前面几篇得知javascript写类无非基于构造函数
和原型
。既然这样,我们写个工具函数来写类。
/**
* $class 写类工具函数之一
* @param {Object} constructor
* @param {Object} prototype
*/
function $class(constructor,prototype) {
var c = constructor || function(){};
var p = prototype || {}; ......
有些网友经常询问:在VC++中如何访问javascript中的对象、函数、变量等元素?
这里把以前发表的一系列文章集中在一起,方便查阅。
vc++访问javascript(1)--window在脚本引擎中的作用
http://blog.csdn.net/pimshell/archive/2008/08/02/2758863.aspx
vc++访问javascript(2)--IDispatchEx是动态脚本语言的基础&nb ......