Use VBScript and JScript on Web pages

Source: Internet
Author: User
Tags array length documentation lowercase object model what scripting

Use VBScript and JScript on Web pages


Author: Winsky Finishing Date: 2004.06.26

Summary: This article discusses the differences between VBScript and JScript, and how they are used. The article also discusses:

l VBScript and JScript in Visual InterDev

L Inline Server scripts and <Script> units

L Script Execution Order

If you want to write a Web page, you have to write some scripts. Fortunately, you can choose one of several scripting languages. Unfortunately, there are so many languages to choose from, what is the choice? What are you supposed to do?

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

In this article, I will discuss these issues. and first, I'll instruct you on how to work with both scripting languages provided by Microsoft.

Microsoft®visual basic®, Scripting Edition (VBScript).

Jscript®, which is the Microsoft version of ECMAScript, is a set of standard definitions that include JScript and JavaScript scripting languages.

Of course, there are other scripting languages, such as Perl and Rexx. But here I highlight two languages, which Microsoft has provided in Internet Explorer and Internet Information Server (IIS) activex®scripting Language (ActiveX Scripting editing language) engine.

I would also like to discuss when you need to use both languages in the same Web page (as you can see, this is entirely feasible). To help you, I've listed a series of small questions that you have to keep in mind when you work.

In order to clearly discuss this issue, I will proceed from the following aspects:

L The difference between VBScript and JScript

How do I determine the scripting language in Visual InterDev?

L apply two languages to the same Web page

L Update Script editing language

L The difference between VBScript and JScript

The biggest difference between VBScript and JScript is their structure. VBScript is a subset of Visual Basic, and you will feel very familiar if you have used Visual Basic or Visual Basic for Applications (VBA). But they're not exactly the same, because VBScript is designed specifically for working in a browser, and it doesn't include features beyond the scope of the script, such as file access and printing, and so on.

JScript, on the other hand, is detached from a set of programming languages such as C, C + +, and Java. If you've ever used C or Java before, then the structure of JScript is very familiar to you. Even if you don't know the language, the structure of JScript is not difficult--it's as convenient as C and Java, not as complex as it is.

Note: In any case, JScript and Java are two completely different languages. Java is a fully fledged development language that can be used for both Web applications and non-Web applications. JScript, however, is a scripting language primarily used for scripting.

The tasks of JScript and VBScript are the same-they allow you to write your own web pages. Each language supports features that are not supported by other languages, but these differences are not sufficient to automatically divide the use of both languages.

More likely, you will choose scripting languages based on a number of other factors, the biggest and most practical reason: Will you be using a platform that supports the language you choose? Are you writing a client or a server? If it is a server, what browser and server software will your users use?

The two scripting languages are described in more detail in the following article, and some guidance is given on what scripting languages to choose in a particular environment.

When do you choose VBScript?

VBScript is mainly used on various platforms of Microsoft Company. So if you want to write in VBScript, you have to use one of the following scripts:

Server script. If you want to write a server script in VBScript (as in an ASP Web page), this server must be a Microsoft Internet Information Server (IIS) or a different server.

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

If you are using Visual InterDev, you should already be using IIS (or the same server), so it is not a problem to write the server script in VBScript. If you're not using IIS, you need to know if the server is capable of supporting VBScript.

However, if you're building a public Web site and you can't control the browsers that other people use, Internet Explorer can only be used for client-side scripting. However, if you are writing client script in VBScript with a known browser, this is still possible.

If your target platform is capable of supporting VBScript, consider the following factors and decide whether to write the script in VBScript:

Error control. In VBScript there is an on Error Resume next to give a description of the special case. If you're writing a server script, error control is very important because the script is run unattended.

Formatted. VBScript has the ability to easily format data, numbers, and currency data.

Event control is handy in Internet Explorer, as in Visual Basic. In VBScript, you can also create an implied event handler that only needs to define a feature in the object __ event structure, such as Button1_onclick. However, in Internet Explorer, JScript does not have this feature (but this feature is supported on other JScript hosts)

When do you choose JScript?

Almost every browser that allows scripting supports JScript. Therefore, if you are writing a widely distributed application for a public Web site, it is a natural choice to write client script with JScript. You'll also find that because JScript is so widely used (and JavaScript similar to it), it's a standard scripting language that you can find in bookstores or on the web for a lot of relevant resources.

If you want to use JScript to write server scripts, you should check to see if your server supports it. Microsoft's IIS allows you to use JScript to write scripts for ASP pages, but not all servers support it.

Other factors that affect your decision are:

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

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

A warning when using JScript: JScript is case-sensitive, and may be difficult to adapt to if you have only used visual Basic or VBScript.

If you are a beginner of JScript, you also need to know how to create an event handler with JScript. In Internet Explorer, JScript asks you to link to an object that is connected to an event handler. Typically, a property can be completed as long as it is identified in the tab bar of an object that has the handler function. As shown in the following example:

On other hosts, you can use an implied event binding, but it requires a slightly different structure than in VBScript. The implied handlers that are agreed in JScript are:

<script language= "JavaScript" >

function Btnsave::onclick () {

Script here

}

</SCRIPT>

It is noteworthy that in these two examples language this property is set to "JavaScript". Internet Explorer knows about the two names "JScript" and "JavaScript", while many other browsers only know "JavaScript" and don't Know "JScript." So, to be on the safe side, if you're not sure that the Web page is browsing with Internet Explorer, you'd better use "JavaScript".

To learn more about setting up event handlers in JScript, check out the Microsoft Visual interdev™ documentation located in MSDN Library online. (You must have MSDN Online membership to access the MSDN Library online, which is free of charge, but simply a one-time registration.)

How do you explicitly use the scripting language in Visual InterDev?

No matter what language you use to write a script, you have to make it clear that the host knows what script engine to use for your script. What language to write depends on whether you're working with an inline server script or working with a <SCRIPT> block (whether it's client-side or server-script), but also depending on the version of the Visual InterDev you're using.

Select scripting language for inline server scripts

If you are using IIS and ASP pages, you can write two types of server scripts: inline scripts (scripts in <%%> delimiters) and scripts in <SCRIPT> units. In general, inline scripting allows you to insert the results into HTML in a Web page. Instead, the scripts in the <SCRIPT> unit are used to establish functions and subroutines that are invoked from inline scripts.

Note: It is possible to create a script in a <SCRIPT> unit and make it run like inline scripts. In other words, the script is not stating a feature or part of a subroutine. But we do not recommend this approach. For more details, see "Server Script Execution Order" later in this article.

For server and client script, the method for selecting a language for a <SCRIPT> unit is the same. For more information, see "Setting up a language for a <SCRIPT> unit" later in this article.

All inline scripts in a Web page are written in the same language. When you create a new ASP page, the Web page automatically contains a @ indicator that identifies the scripting language of the inline script. @ instructions can be like this:

<%@ language= "VBScript"%>

@ indicates that it must be the first inline script command in the Web page. Without this indication, IIS specifies VBScript as the inline scripting language by default.

Change the inline scripting language for a Web page

In Visual InterDev 1.0, you can edit the page to change the instructions and set up a new language, such as "JavaScript." Wait

In Visual InterDev 6.0, when you right-click a Web page, the Properties dialog box for that page appears. Then, under the Default scripting language, select a new language. After being applied, the editor is updated to the appropriate scripting language in the @ directive. Of course, if you want, you can also manually edit @ instructions yourself.

You can also replace the default values for all new ASP pages. This will not change the current Web page, but it will be useful for all the pages you have built from now on.

Replace the default language for inline scripts

In Visual InterDev 1.0, select the Options option from the Tools menu, and then select the HTML column. Select a language at the default scripting language at active Server pages.

In Visual InterDev 6.0, right-click the item name in Project Explorer and select Properties. Select the editor Defaults column, and then select a new default at the default scripting language.

Set Language for <SCRIPT> unit

Unlike an inline server script, the scripting language you use in the <SCRIPT> unit can have many different kinds of web pages. If a piece is written in VBScript, then another piece can also be in JScript. (Remember, not all platforms support VBScript.) )

Note: There are a number of considerations for mixing languages in the same Web page, and for details, see "Working in several different languages on the same page" later in this article.

Specify a language for a <SCRIPT> unit

The <SCRIPT> tag contains a language unit that indicates the language you want to use. For example, the following <SCRIPT> tag indicates that all scripts in the <SCRIPT> unit are written in JScript:

<script language= "JavaScript" >

function Test () {

Alert ("Testing");

}

</SCRIPT>

Note: The language attribute is required in the server script for IIS. In client script, it's just an option. If the language attribute is not available, the handler uses the default scripting language. In the client script for Internet Explorer, the default value is usually the scripting language for the first <SCRIPT> unit, or JScript if no language has been specified. We recommend that you do not rely on default values, preferably a scripting language for <SCRIPT> units.

In a <SCRIPT> unit, you can't mix VBScript with JScript. If you want to use them at the same time, you must have at least two <SCRIPT> units on your Web page.

Visual InterDev 1.0 and Visual InterDev 6.0 provide tools to automate the generation of scripts. There are Script Wizard in Visual InterDev 1.0 to help you. In Visual InterDev 6.0, there are the following tools for generating scripts:

L Script block command. As long as you right-click on the page, this command appears in the menu. It can insert the frame of a <SCRIPT> unit at that point in your point.

L Script outline command. When you double-click the name of the event, it creates a framework for the event controller. Based on your settings, different script generation tools will determine the language you want to use. You can change the default language type for scripting tools on individual pages.

Set the language for the Script Wizard in Visual InterDev 1.0

Select the options option from the Tools menu and select the HTML bar. Select a language in the default scripting language of the Script Wizard.

Set a language for the script generation tool in Visual InterDev 6.0 for a Web page

First, right click on the page, the Web Page Properties dialog box appears. Then 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 that are made for generating server script blocks can be used to set the language for inline scripting. If you want to use one language in an inline script, and you use a different language in the server script block generated by the tool, you can only change the language manually.

You can also change the default values for your project so that all of the pages you build will have the same default language settings as the Scripting tools.

Change the default language settings for the script Generation tool in the Visual InterDev 6.0 project

Right-click the project's name in Project Explorer and choose Properties. Select Editor Defaults to select a new default at the default scripting language.

Note: The generated server script block language setting method can also be used to set the language for inline scripts. If you want to use one language in an inline script, and you use a different 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, check the "Choosing a scripting Language" in the MSDN Library online and the Visual InterDev 6.0 documentation. Handling Events with HTML Elements ".

Use several languages at the same time in the same Web page

I think I've made it clear that you can use different scripting languages on the same Web page. Let's recap: Why do you want to do this? Here are a few reasons:

The first reason is that you just want to take advantage of a language in a program, and other parts of the script are written in another language.

The second reason: Maybe the existing code you're dealing with is written in a language you're unfamiliar with. For example, design time control is written in JScript, and the language you're familiar with is VBScript. In most cases, you can mix several scripting languages on the same Web page and they can work well with each other. Scripts written in one language can invoke programs written in another language and can share global variables. But sometimes there's a little problem, and now I'm going to explain it.

Parentheses in a VBScript call

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

RetVal = Calljsfunction ()

If you forget the braces, the value returned by the function is not the value you want, but a goal that contains the function itself.

Order of execution of server scripts

Inline server scripts run sequentially, from beginning to end. You can define a callable program (function or subroutine) in a server script so that you can call them anytime you want.

All inline scripts must be written in the same language, which is the scripting language specified in the @ Directive in front of the page. Therefore, you cannot mix several scripting languages in an inline script.

You might say, "Wait a minute." In theory, it is absolutely possible to put inline scripts in a <SCRIPT> unit-that is, if the script in the cell is not a function or a subroutine. Just like 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 do that. However, you will be controlled by the order in which the IIS ASP handlers are executed. For example, if you build a server script and run it in IIS 4.0, you'll find that the order of execution is this:

• <SCRIPT> unit scripts in non-default languages

L Inline Script

L <SCRIPT> Unit script with default language

The order above depends heavily on the loading order of the ActiveX language engine, while the ASP handlers in IIS may change in the future, so we don't want you to work in that order, whether you're using a <SCRIPT> unit for functions or subroutines, Or is it just for the execution of the sequence of irrelevant encodings.

Order of script blocks

When you mix several languages together, the order in which the,<script> blocks appear in the Web page is important and sometimes affects whether they work properly or not. Take a look at this simple example of calling 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 won't do any work. To be more clear, document write instructions will write an empty string to the Web page. Why? Quite simply, the following JScript <SCRIPT> block is not read and parsed without parsing, so the Web page cannot use it when dealing with VBScript blocks. And when the browser processes the script block in the Web page, it goes down from the top.

In this example, you can solve this problem by simply changing the order of the script blocks. In fact, this kind of problem is not so common, in most cases the functions and subroutines included in,<script> can only be invoked when the entire page is downloaded and all the cells are available. However, you still have to remember that the page is processed in a certain order, the different languages of the <SCRIPT> blocks are handled separately.

Case sensitivity and naming conventions

JScript distinguishes between uppercase and lowercase: In each JScript keyword, as well as in all parts of the JScript namespace, you can use uppercase and lowercase letters in a proper combination. This includes all the keywords in the object model that you use. For example, when you use an ASP to response an object, you must use uppercase or lowercase letters (such as Response.Write) correctly, or JScript will resolutely deny it. Similarly, if you write a handler for a DHTML onclick event, JScript may not admit it if you use a form that is often used in Visual Basic.

VBScript, of course, does not distinguish between uppercase and lowercase letters. In a way, this disregard for capitalization is even useful when you have access to a unit of JScript written in VBScript. Two scripts are given in the following few small examples. The first is written in JScript, which also includes a global variable (CTR) and a small function (doubleme). The second script, written in VBScript, is a button handler that can call a JScript function and report the value of the global variable.

<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's obvious that I didn't use the letter case correctly when I mentioned the Doubleme function and the CTR variable, but the program still works well.

That's a good news. The bad news is that in these cases you must pay attention to the case of the letters:

If a JScript script in a Web page contains cells that can only be distinguished by uppercase and lowercase letters, for example, a function called timer () and a global variable called a timer, and so on. Remember, although this encoding is not very good, sometimes in your Web page you may still encounter the original encoding.

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

If JScript includes cells that can only be distinguished by the case of letters, VBScript will let you look at them the same way (that is, do not distinguish between uppercase and lowercase). VBScript can only look at the difference between the case, they are not the same, so it recognizes only one JScript unit. In other words, if VBScript first finds a function called a timer (), it cannot find a global variable called a timer, and vice versa. Even if you pay attention to the difference in VBScript coding, the result is the same. Because VBScript thinks they have only one way of writing when dealing with these names. Unfortunately, there is no other workaround, unless the small differences between these uppercase and lowercase are not used in JScript's code to distinguish different function names.

It is not uncommon to use a cell name as a parameter, except in some special cases. One of the special cases is in the Script object model in Visual InterDev 6.0. I'll use it as an example, and you can think of it as a processing guideline in the same situation.

The Scripting Object model for Visual InterDev 6.0 is written in JScript. You can also write your own code in VBScript, which can be used in conjunction with a scripting object model written in VBScript case sensitivity. Sometimes you can't call a method directly unless you pass the name of the method to a JScript function, and the function calls this method for you.

A typical example is the warning method, which you can use to bind a method to a particular event. In the following example, the onMouseOver event for a button is bound to the Changecaption () event handler.

<script language= "VBScript" >function setadvisemethods ()

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

End Function

</SCRIPT>

Because you send these names as strings to a JScript function, you can get the correct case representation. Later, JScript automatically evaluates the names with its case-sensitive rules, and if the capitalization is incorrect, it will not be able to find them.

To transfer an array from VBScript to JScript

Sometimes a VBScript program transmits an array as one of its arguments, 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 can simply create a VBArray object in a JScript function and convert it into a JScript array with ToArray.

Note: JScript does not support multidimensional arrays, and if the original VBScript array is multidimensional, it becomes a one-dimensional JScript array after toarray conversion.

In the example below, there is a VBScript script that creates an array and a JScript script that will show you how to get 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), the converse is not possible. That means you can't convert a JScript array into a VBScript array. If you are encountering a JScript program, to convert an array, you can choose several approaches:

L WRITE the calling program in JScript

• Re-write JScript programs in VBScript

L if possible, convert a JScript array into a different structure, such as converting to a restricted string, and so on, so that VBScript can be used. For example, you can use the ToString function to convert an array into a comma-delimited, unrestricted string. So, in VBScript you can use the Split function to divide it into different units. It is obvious that this method does not work in many cases, but it does.

Update script language

Both VBScript and JScript are not immutable languages. The engines of both languages are updated regularly, adding new features to solve the problems that have been discovered. Since both languages are actually applied as ActiveX components, you can update any controller at the same time whenever you update it. When you have a new language engine installed, any application that needs to invoke the engine calls the new engine when they run certain scripts-these include Internet Explorer or Visual InterDev.

For information about the language engine and how to update it, as well as the general knowledge of scripts, visit the Microsoft Scripting Web site at: http://msdn.microsoft.com/scripting/. Also, I wish you all the best in scripting!

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.