Use VBScript and JScript on Web pages

Source: Internet
Author: User

Use VBScript and JScript on Web pages


Prepared by: winsky sorting Date: 2004.06.26

Summary: This article discusses the differences between VBScript and JScript and their usage scenarios. This article also discusses:

L VBScript and JScript in Visual InterDev

L inline server scripts and <Script> units

L script execution sequence

If you want to write a Web page, you must write some scripts. Fortunately, you can select one of several scripting languages. Unfortunately, which of the following languages can be selected? What should you do?

Or, if you get a Web page, its script is written in a scripting language that you don't use very much, how can you use this script to add some new features?

In this article, I will discuss these issues. First of all, I will guide you how to use the two scripting languages provided by Microsoft.

Microsoft®Visual Basic®, Scripting Edition (VBScript ).

JScript®It is the Microsoft version of ECMAScript and is a set of standard definitions including JScript and JavaScript scripting languages.

Of course, there are other scripting languages, such as Perl and Rexx. However, here I will focus on two languages. Microsoft provides ActiveX for them in Internet Explorer and Internet Information Server (IIS ).®Scripting Language (ActiveX script editing Language) engine.

I also want to discuss when you need to use these two languages on the same web page (as you can see, this is completely feasible ). To help you, I listed a series of related small issues that must be kept in mind during your work.

To clearly discuss this issue, I will start from the following aspects:

L differences between VBScript and JScript

L how to determine the script language in Visual InterDev?

L apply two languages to the same web page

L update the script editing language

L differences between VBScript and JScript

The biggest difference between VBScript and JScript is their structure. VBScript is a subset of Visual Basic. If you have used Visual Basic or Visual Basic for Applications (VBA), you will feel very familiar with it. However, they are not exactly the same, because VBScript is specially designed to work in a browser. It does not include some features beyond the scope of the script, such as file access and printing.

On the other hand, JScript is separated from a group of programming languages such as C, C ++, and Java. If you have used C or Java before, you will be very familiar with the JScript structure. Even if you do not know these languages, the structure of JScript is not difficult at all-it is as convenient as C and Java, but not as complicated as they are.

Note: In any case, JScript and Java are completely different languages. Java is a completely mature development language that can be used for web applications and non-web applications. JScript is a script language mainly used for scripting.

The JScript and VBScript tasks are the same-they allow you to write your own Web pages. Each language supports features not supported by other languages. However, these differences are not enough to automatically divide the use range of the two languages.

More likely, you may choose a scripting language based on some other factors. The biggest reason is the most practical reason: Can the platform you are about to use support your selected language? Are you writing a client or a server? If it is a server, what browser and server software will your users use?

The following article describes these two scripting languages in detail, and provides some guidance on the scripting language selected in a special environment.

When should I select VBScript?

VBScript is mainly used on various platforms of Microsoft. Therefore, if you want to use VBScript for writing, you must use one of the following scripts:

Server script. If you want to use VBScript to write Server scripts (such as on an ASP Webpage), the Server must be Microsoft Internet Information Server (IIS) or another Server.

Client script. If you want to use VBScript in a browser script, this browser must be a version of Internet Explorer. Other popular browsers may not support VBScript.

If you are using Visual InterDev, you should already be using IIS (or other identical servers). Therefore, Using VBScript to write server scripts is not a problem. If you are not using IIS, check whether the server supports VBScript.

However, if you want to create a public Web site and you cannot control the browsers used by others, Internet Explorer can only be used for client scripts. However, if you use VBScript to write client scripts as the browser knows, this is feasible.

If your target platform supports VBScript, consider the following factors before deciding whether to use VBScript to write the script:

Error Control. In VBScript, there is On Error Resume Next to explain the special situations. If you write a server script, error control is very important, because the script runs unattended.

Format. VBScript provides this function to easily format data, numbers, and currencies.

In Internet Explorer, event control is very convenient, just like in Visual Basic. You can also create an implicit event handler in VBScript. You only need to define a function in the form of an object _ event structure, such as Button1_onclick. However, in Internet Explorer, JScript does not provide this function (but it is supported on other JScript hosts)

When should I select JScript?

Almost all browsers that allow scripting support JScript. Therefore, if you write a widely distributed application for a public Web site, it is natural to use JScript to write client scripts. You will also find that, because JScript is widely used (and similar JavaScript), it is a standard scripting language, you can go to the bookstore, you can also find many related resources on the Internet.

If you want to use JScript to write server scripts, check whether your server supports it. Microsoft's IIS allows you to use JScript to compile ASP Web page scripts, but not all servers support this.

Other factors that affect your decision include:

Dynamic execution. A powerful feature of JScript is that it allows you to create and execute scripts in your scripts or evaluate them dynamically. In short, you can write scripts in your scripts. This feature is very useful when you work with DHTML, because you can dynamically operate the DHTML document model.

Object-oriented. JScript uses a prototype-based object structure that allows you to define objects in scripts. You can add methods and features to the object prototype to expand the built-in objects and custom objects.

A warning when using JScript: JScript is case sensitive. If you have only used Visual Basic or VBScript, it may be difficult to adapt.

If you are a beginner in JScript, you also need to know how to use JScript to create an event handler. In Internet Explorer, JScript requires you to link to an object, which is connected to the event handler. Generally, you only need to specify its attribute in the tag bar of the object that identifies the event and has the handler function. For example:

On other hosts, you can use implicit event binding, but the structure required by it is slightly different from that in VBScript. The implicit handler agreed in JScript is:

<Script language = "JavaScript">

Function btnSave: onclick (){

// Script here

}

</SCRIPT>

It is worth noting that in these two examples, the LANGUAGE attribute is set to "JavaScript. Internet Explorer knows "JScript" and "JavaScript", while many other browsers only know "JavaScript" and do not know "JScript ". Therefore, for the sake of security, if you are not sure that the webpage is viewed using Internet Explorer, you 'd better use JavaScript ".

For more information about how to create an event handler in JScript, see Microsoft Visual InterDev in MSDN Library Online.™Documentation. (To access the MSDN Library Online, you must have the MSDN Online Membership qualification. This qualification is free of charge, but you must register it at once.

In Visual InterDev, how does one specify the scripting language used?

No matter what language you use to write scripts, you must clearly point it out so that the host knows what script engine is used to process your scripts. The language used for writing depends on whether you work with an inline server SCRIPT or a <SCRIPT> block (whether it is a client SCRIPT or a server SCRIPT ), it also depends on the version of Visual InterDev you are using.

Select script language for inline server scripts

If you are using IIS and ASP Web pages, you can write two types of server scripts: inline scripts (scripts in the <%> separator) and scripts in the <SCRIPT> unit. In general, you can insert the result into the HTML of the webpage using an inline script. Instead, scripts in the <SCRIPT> unit are used to create functions and subprograms called from inline scripts.

Note: it is also possible to create a SCRIPT in the <SCRIPT> unit and make it run like an inline SCRIPT. That is to say, what the script states is not a function or a part of a subroutine. However, we do not recommend this approach. For more details, see the "server script execution sequence" in the later part of this article ".

For server and client scripts, you can select the same language for the <SCRIPT> unit. For more information, see "set language for a <SCRIPT> unit" in the later part of this article ".

All inline scripts on a web page must be written in the same language. After you create a new ASP Web page, the page automatically contains a @ directive, which specifies the script language of the inline script. @ Indicates that it can be like this:

<% @ Language = "VBScript" %>

@ Indicates that it must be the first line of inline script command on the webpage. Without this instruction, IIS uses VBScript as the inline scripting language by default.

Change the inline scripting language of a web page

In Visual InterDev 1.0, You can edit the webpage to change the instruction and set a new language, such as "JavaScript.

In Visual InterDev 6.0, right-click a webpage and the Properties dialog box of the webpage appears. Select a new language under the Default scripting language. After the application, the Editor updates the script language in the @ directive. Of course, if you want to, you can manually edit the @ directive.

You can also change the default value for all new ASP Web pages. This operation cannot change the current webpage, but it will be useful to all webpages you have created from now on.

Change the default language for inline scripts

In Visual InterDev 1.0, select Options from the tool menu, and then select the HTML column. Select a language in the default scripting language of Active Server Pages.

In Visual InterDev 6.0, right-click the Project name in Project Explorer and select Properties. Select the Defaults column of the Editor and select a new default value in the default scripting language.

Set language for <SCRIPT> units

Unlike the inline server SCRIPT, the SCRIPT language you use in the <SCRIPT> unit can be in the same web page. If one part is written in VBScript, the other part can also be written in JScript. (Remember, not all platforms support VBScript .)

Note: There are many considerations for mixing multiple languages on the same web page. For details, refer to "working in several different languages on the same web page" in the later part of this article ".

Specify a language for a <SCRIPT> Unit

The <SCRIPT> tag contains a LANGUAGE unit that specifies the LANGUAGE you want to use. For example, in the <SCRIPT> MARK below, all scripts in the <SCRIPT> unit must be written in JScript:

<Script language = "JavaScript">

Function test (){

Alert ("testing ");

}

</SCRIPT>

Note: The LANGUAGE attribute is required in the IIS server script. In the client script, it is only an option. If the LANGUAGE attribute does not exist, the handler uses the default script LANGUAGE. In Internet Explorer client scripts, the default value is usually the SCRIPT language of the first <SCRIPT> unit, or if no language is specified, it is JScript. We recommend that you do not depend on the default value. It is best to set a SCRIPT language for the <SCRIPT> unit.

In a <SCRIPT> unit, you cannot mix VBScript and JScript. If you want to use them at the same time, you must have at least two <SCRIPT> units on your webpage.

Both Visual InterDev 1.0 and Visual InterDev 6.0 provide tools to automatically generate scripts. Script Wizard is available in Visual InterDev 1.0 to help you. Visual InterDev 6.0 has the following script generation tools:

L Script Block command. As long as you right-click the webpage, this command will appear in the menu. It can insert a <SCRIPT> Unit framework at the point you clicked.

L Script Outline command. When you double-click the event name, it will create an event controller framework. Based on your settings, different script generation tools determine the language you want to use. You can change the default language type for the script tool on some web pages.

Set language for Script Wizard in Visual InterDev 1.0

Select Options from the Tools menu, and then select the HTML bar. Select a language from the default Script language of Script Wizard.

In Visual InterDev 6.0, set a language in the web page for the script Generation Tool

First, right-click the webpage, And the Properties dialog box of the webpage appears. Select a new language under the default scripting language. You can set the same language for server scripts and client scripts.

Note: The same settings made to generate server script blocks can be used to set language for inline scripts. If you want to use one language in the inline script, and use another language in the server script block generated by the tool, you can only change the language manually.

You can also change the default value of your project, so that all the web pages you create will have the same default language settings as the script tool.

Change the default language settings of the script generation tool in the Visual InterDev 6.0 Project

In Project Explorer, right-click the Project name and select Properties. Select Editor Defaults and select a new default value in the default scripting language.

Note: The method for setting the generated server script block language can also be used to set the language for inline scripts. If you want to use one language in the inline script, and use another language in the server script block generated by the tool, you can only change the language manually.

For more information about the script generation tool in Visual InterDev 6.0, see "Choosing a Scripting Language" in MSDN Library Online and "Handling Events with HTML Elements" in Visual InterDev 6.0 ".

Use several languages on the same webpage at the same time

I think I have already made it clear that different scripting languages can be used on the same web page. Let's review: Why do you want to do this? There are several reasons:

The first reason is: you just want to use the advantages of a language in a program, and the other part of the script is written in another language.

The second reason is that the current encoding you are facing may be written in a language you are not familiar. For example, the design time control is written in JScript, but the language you are familiar with is VBScript. In most cases, you can mix several scripting languages in the same web page and they can work well with each other. Scripts written in one language can call programs written in another language and share global variables. However, there may still be some minor issues. Now I will explain this.

Brackets in a VBScript call

When you call a JScript function from VBScript, note that brackets are required during the call, even if this function does not require parameter setting. For example, a call should look like this:

RetVal = callJSFunction ()

If you forget to add parentheses, the value returned by this function is not what you want, but a target that contains the function itself.

Execution sequence of server scripts

Inline server scripts run sequentially from start to end. You can define a callable Program (function or subroutine) in a server script so that you can call them at any time if necessary.

All inline scripts must be written in the same language, that is, the script language specified in the @ directive before the webpage. Therefore, you cannot mix several scripting languages in an inline script.

You may say "wait ". Theoretically, it is absolutely possible to put an inline SCRIPT in the <SCRIPT> unit-that is, as long as the SCRIPT in the unit is not a function or a sub-program. As in the following example:

<% Response. Write ("Some inline script <BR>") %>

<Script language = "VBScript" RUNAT = "Server">

Response. Write ("Script in a SCRIPT element <BR> ")

</SCRIPT>

Yes, you can. However, you will be controlled by the execution sequence of the iis asp processing program. For example, if you create a server script and run it in IIS 4.0, you will find that the execution order is as follows:

L use non-default <SCRIPT> unit scripts

L inline script

L use the <SCRIPT> unit SCRIPT in the default language

The preceding sequence depends largely on the loading sequence of the ActiveX language engine. At the same time, ASP processing programs in IIS may change in the future. Therefore, we do not want you to work in this order, whether you only use the <SCRIPT> unit for functions or subprograms, or simply use it for code that is irrelevant to the execution order.

Script block Sequence

When you use multiple languages together, the order in which <SCRIPT> blocks appear on a webpage is very important, and sometimes it affects whether they work normally. Let's take a look at the following simple example. It is used to call a function written in JScript in an inline VBScript script:

<Script language = "VBScript">

'Calls a JScript function

ANumber = 2

DoubledNumber = doubleMe (aNumber)

Document. write ("The answer is" & doubledNumber)

</SCRIPT>

<Script language = "JavaScript">

Function doubleMe (aNumber ){

Return aNumber * 2;

}

</SCRIPT>

It does not play any role. To be more clear, the document writing instructions will write an empty string to the webpage. Why? When processing VBScript blocks, the following JScript <SCRIPT> blocks are not read and have no syntax analysis. Therefore, webpages cannot use them. When the browser processes the script block in the webpage, it proceeds from top to bottom.

In this example, you only need to change the sequence of script blocks to solve this problem. In fact, this type of problem is not that common. In most cases, <SCRIPT> Functions and subprograms can be called only when the entire web page is downloaded and all units can be used. However, you still need to remember that web pages are processed in a certain order, and <SCRIPT> blocks in different languages are processed separately.

Case Sensitive and naming conventions

JScript can distinguish between uppercase and lowercase letters: In the keywords of each JScript, and all the parts of the JScript name space, you can properly combine uppercase and lowercase letters for use. This includes all the keywords in the object model you are using. For example, when you use an ASP Response object, you must correctly use uppercase or lowercase letters (for example, Response. Write); otherwise, JScript will reject the request. Similarly, if you write a processing program for the DHTML onclick event, JScript may not be recognized if it is often used in Visual Basic.

VBScript certainly does not distinguish between upper and lower case letters. To some extent, this case-insensitive neglect applies even when you use VBScript to access the units written in JScript. The following example shows two scripts. The first one is written in JScript, which also includes a global variable (ctr) and a small function (doubleMe ). The second script is written in VBScript. It is a button processing program that can call the JScript function and report the value of global variables.

<Script language = javascript> var ctr; function doubleMe (aNumber) {// Initialize value of global counter if (parseInt (ctr )){

Ctr = ctr + 1 ;}

Else {

Ctr = 1;

}

Return aNumber * 2;

}

</SCRIPT>

<Script language = "vbscript">

Function btn1_onclick ()

NumberToDouble = 12

Alert ("The doubled number =" & DoubleMe (numberToDouble ))

Alert ("You have doubled the number" & Ctr & "times .")

End function

</SCRIPT>

In the VBScript script, it is obvious that when I mention doubleMe functions and ctr variables, the uppercase and lowercase letters are not correctly used, but the program is still running well.

This is good news. The bad news is that you must pay attention to the uppercase and lowercase letters in the following cases:

If the JScript script in a webpage contains only uppercase and lowercase letters, for example, a function called Timer () and a global variable called timer. Remember, although this encoding method is not very good, sometimes you may encounter an existing encoding on your webpage.

If you use the JScript unit name as a string parameter in a function call

If the units included in JScript can only be distinguished by uppercase and lowercase letters, VBScript treats them as the same (that is, they do not distinguish uppercase and lowercase letters ). VBScript can only check whether they are the same regardless of case. Therefore, it only recognizes one JScript unit. In other words, if VBScript first finds a function called Timer (), it cannot find the global variable called timer, and vice versa. Even if you have made the difference between them in VBScript encoding, the same is true. VBScript considers that there is only one way to write these names. Unfortunately, there is no other solution here, unless the tiny differences between the upper and lower case are not used in JScript code to distinguish different function names.

Except in some special cases, it is not common to use the unit name as a parameter. In special cases, it is in the script Object Model in Visual InterDev 6.0. I will use it as an example below. You can regard it as a processing criterion in the same situation.

The script object models of Visual InterDev 6.0 are all written in JScript. You can also use VBScript to compile your own code. It can be used together with the script object model written in case-sensitive VBScript. Sometimes, you cannot directly call a method unless you pass the method name to a JScript function, and the function calls this method for you.

A typical example is the warning method. You can use it to bind a method to a special event. In the following example, the onmouseover event of a button is bound to the changecaption () event handler.

<Script language = "VBScript"> Function setAdviseMethods ()

Btn1.advise ("onmouseover", "changecaption ()")

End Function

</SCRIPT>

Because you transmit these names as strings to a JScript function, you can get the correct case representation. Later, JScript automatically uses its case-sensitive rules to reevaluate these names. If the case is incorrect, it cannot find them.

Transfers an array from VBScript to JScript

Sometimes a VBScript program transmits an array as its parameter or as its return value. You can call this program from JScript, but you must convert the VBScript array into an available JScript array. You only need to create a VBArray object in the JScript function and convert it into a JScript Array Using toArray.

Note: JScript does not support multi-dimensional arrays. If the original VBScript array is multi-dimensional, it will become a one-dimensional JScript array after toArray conversion.

In the following example, A VBScript script creates an array and a JScript that describes how to obtain and use this array:

<Script language = "VBSCRIPT">

Function makeArrayVB ()

'Creates a VBScript array

Dim anArray (1, 1)

AnArray (0, 0) = "0, 0"

AnArray (0, 1) = "0, 1"

AnArray (1, 0) = "1, 0"

AnArray (1, 1) = "1, 1"

MakeArrayVB = anArray

End Function

<Script language = "JavaScript">

// Accesses a VBScript array within a JScript script

Function getVBArray (){

Var arrayObj;

Var jsArray;

ArrayObj = makeArrayVB ();

JsArray = VBArray (arrayObj). toArray ();

Alert ("VBScript array length =" + jsArray. length );

// Displays the contents of the array

For (I = 1; I <= jsArray. length; I ++ ){

Alert (jsArray [I-1]);

}

}

</SCRIPT>

However, in the current VBScript (4.0), this cannot be done in turn. That is to say, you cannot convert a JScript array into a VBScript array. If you encounter a JScript program and want to convert the array, you can select the following methods:

L use JScript to write the calling program

L use VBScript to re-compile the JScript Program

L if possible, convert the JScript array into a different structure, such as converting it into a restricted string, so that VBScript can be used. For example, you can use the toString function to convert an array into an unrestricted string separated by commas. In this way, you can use the Split function in VBScript to divide it into different units. Obviously, this method does not work in many cases, but sometimes it does.

Update script language

VBScript and JScript are not static languages. The engines of the two languages are updated on a regular basis to add some new features to solve the problems that have been found. Because both languages are actually applied as ActiveX components, you can update them at the same time whenever you update any controller. After you install a new language engine, any application that needs to call the engine will call this new engine when they run some scripts-these engines include Internet Explorer or Visual InterDev.

To learn about the language engine and how to update it, as well as general script knowledge, visit the Microsoft Scripting Web site at http://msdn.microsoft.com/scripting /. At the same time, I wish you all the best in writing the script!

Related Article

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.