There are two conditions that are essential to learn a programming language well. One is to combine theory with practice, and verify the theory in the book through practical routines can deepen your understanding of the theory; the other is to learn to summarize and apply it. Write down your experience and experience as an experience or lesson and refine it in future applications, which will definitely improve your understanding of this programming language. The following are two experiences in learning and using Asp programming, I hope it will be helpful to everyone.
Interaction between VBScript and JScript in ASP page
ASP has the ability to manage scripting programs in different languages and can automatically call appropriate scripting engines to interpret script code and execute built-in functions. The ASP development environment provides two scripting engines, namely VBScript (default) and JScript. However, developers are not limited to using only these two languages, and can use any scripting language as long as they can provide the appropriate ActiveX scripting engine.
The choice of scripting language is often based on many different reasons: it is probably the language that developers are most familiar with, maybe the one that provides the most featured support for a given project, or maybe the one that is most efficient. Different environments and requirements make us pay attention to different factors when choosing scripting languages, and at some point we face the problem that the selected scripting language cannot directly provide functions in other languages, or a script has been written but It uses another scripting language.
What should I do now? Do you need to rewrite these scripts in the current script language? Or, is it possible to call built-in functions of other scripting languages in one scripting language? What this article wants to explain is how to interact with VBScript scripts and JScript scripts in ASP applications to maximize the special support of the two scripting languages.
1. Built-in functions for VBScript and JScript
In VBScript and JScript, there are a large number of built-in functions that are the same or similar. However, functions built into one scripting language do not always have corresponding functions in another scripting language. For example, VBScript provides many functions for manipulating strings and formatting data, which do not exist in JScript. These functions include StrReverse(), Filter(), FormatCurrency(), etc. On the other hand, the functions provided by JScript for managing arrays, string encoding, etc. are not defined in VBScript, such as join(), reverse(), pow(), bit operations, escape() and unescape(). wait.
So, what should I do if I need a VBscript function in a JScript program?
2. Mutual calls of heterogeneous scripts
If you need to call a built-in VBScript function in a JScript script, you should write a VBScript user-defined function (called VBScript built-in functions here), and then call this user-defined function in the JScript script just like calling a public JScript function.
For example, if the VBSCript built-in function to be called is FormatCurrency(), you can declare the following custom function:
< SCRIPT LANGUAGE=VBSCRIPT RUNAT=SERVER>
Function FormatValue(Value)
FormatValue = FormatCurrency(Value)
End Function
< /SCRIPT>
Next, in JScript code, you can call FormatValue() like ordinary JScript functions. VBScript code calls JScript functions can also be implemented using a similar method.
Applying the same rules, we can call any user-defined function within any script. However, when calling a VBScript procedure (Sub) without parameters from a JScript script, you should pay attention to it. At this time, you should call it in JScript like calling a JScript function without parameters, such as calling VBScript Sub with foo() foo process.
3. Data Sharing
It is very useful in some cases to mix VBScript and JScript functions, but it can also be useful to share data between scripts in different languages. The way to implement this sharing is simple: no matter what language you are using, variables declared at the page level can be referenced arbitrarily.
The usage methods of objects are also similar. You can use a suitable language to read, modify properties or call objects. Of course, the properties and methods of a given object are defined by the language that created the instance of that object. As in the above example VBScript procedure call, when a method of a VBScript object without parameters is called from JScript, its calling method also follows the JScript calling rules, and vice versa.
IV. Array Management
The problem of array sharing is a little more complicated. Although arrays can also be shared among scripts in different languages like other variables, it is necessary to pay attention to compatibility issues.
VBScript arrays can be referenced with VBScript symbols in JScript, that is, referring to array elements with myArray(2) instead of JScript's array elements referencing the symbol myArray[2]. In addition, you can also use a special JScript object - VBArray object to convert VBScript arrays into JScript arrays. The following code creates a JScript array myJSArray from the VBScript array myVBArray:
var Temp = new VBArray(myVBArray)
var myJSArray
myJSArray = Temp.toArray()
The above code first creates a temporary VBArray object and then converts itself into a JScript array using its toArray() method. After that, myJSArray can be used like ordinary JScript arrays, such as myJSArray[1]. But it should be noted that the toArray() method will convert a multi-dimensional VBArray into a one-dimensional JScript array.
Referring to JScript arrays from VBScript is more complex. Although in VBScript we can directly access methods and properties related to JScript arrays, there is no way to directly access a single element of a JScript array. That is, we can read the length property of the JScript array in a VBScript script as follows:
x = myJSArray.length
But the single element of the array cannot be read directly, the following VBScript code is incorrect:
x = myJSArray(3)
A possible way to solve this problem is to perform a conversion process, as shown in the following code, where VBScript is assumed to be the default scripting language:
< %
Dim Temp
Dim myVBArray
Temp = myJSArray.join(, )
myVBArray = Split(Temp, , )
%>
The JScript join() method here converts the array myJSArray element to a string with a comma as a splitter, and the VBScript Split() function converts the string to a VBScript array. Note that we are calling the join method of JScript in the VBScript environment. According to this example, we can simulate the toArray() method of the VBArray object of JScript through custom VBScript functions to implement the conversion of JScript array to VBScript array.
Create dynamic ASP pages with templates
Regarding templates, I think everyone may have some concepts. In Word, many templates are designed with a general layout, just fill in the placeholder characters in your own words. This is probably the meaning of the template here. The relatively stable part of the page is fixed, and other parts are inputting different contents in different situations. In fact, there is also a template function in Dreamweaver, but the static one can only fill content manually, and here is about dynamic automatic content filling.
First, let me explain why the template file is used. Sometimes, templates can give you a more complete concept of web page functions and layout. When you see the template format of Word, you will know what the final layout looks like, and the same is true here. For example, you can keep ASP statements and use different templates to create different page styles. In this way, you don’t have to write different ASP pages for each different style of web page, which obviously saves us a lot of time and energy. Moreover, template files can make it easier for you to browse page codes, and you don’t have to worry about the mix of ASP and HTML that makes you dizzy. You can just focus on HTML without having to worry about ASP at all. Another thing is that the touchpad is very simple and you will definitely understand it quickly. In this article, a database will be used - a widely used employee table. Includes employee ID, name, photo, as well as job summary and footnotes for photos. Here is the structure of this access database:
File name - myDatabase.mdb
Table name - Employees
ID
Automatic counting (Autonumber)
FullName
Text - up to 100 characters
PicURL
Text - up to 255 characters
Duties
Comment Type
PicCaption
Text - up to 50 characters
A very simple database, right? Of course you can expand it as required, and that's your own business. I assume you already understand the basic operations of the database, so I don’t have to spend too much effort on it. In fact, the code here is very simple. As long as you know some basic things, you can understand it. After establishing the database, we can start creating the template file. This file is the skeleton of each page. I didn't use the form, that was a bit troublesome, and I was lazy :-) And, don't add, tags, because that's what to add to the ASP page. So in the end, it looks like this:
Full Name:
%name% < br>Description of duties:
%duties%
This picture was taken: %date%
Employee ID: %empID%
That's it! This is a simple template. Save it as template.tmp and will reference it in the ASP page in the following. It should be noted that we can add various HTML tags to the template. You can completely define the structure and style of the web page in the touch version, just like actually writing a web page, just to mark the key points - noticed Are those % surrounded? That's the essence of the template. Perhaps you have noticed that those % are surrounded by corresponding to the meaning of the field in the database. Take a look at the ASP code below, how to read the template and database files, knead them together, and then output the HTML page we want.
That's all the code, it's very simple, isn't it? All it does is to open the template file, read each line in order, and then replace the %img% and %name% tags in the template with the actual field values read in the database. Moreover, it interprets the carriage return in a large piece of text in the Duties field as the carriage return of HTML
, so as not to disrupt the entire layout format. How about it? It's easy to do it. Template can really play a miraculous role in some applications, saving time and effort. In theory, you can also modify this code and use FileSystemObject to read and write text, so that the data does not have to be stored in the database. In some applications, such as instant news releases, this may be more convenient.