Differences between JS and vbs

Source: Internet
Author: User

Vbs comes from VB, while JavaScript uses Java syntax. I won't talk about it here!

Vbs does not need to be case-sensitive, while JS needs to be case-sensitive. I believe this is why many people hate Js. I used to hate JS too much. However, after learning vbs for a while, when I used vbs for Web pages, I found that many areas were unsatisfactory and gradually got used to the JS syntax.

JS can also exist independently like vbs:

// JS
VaR FSO = new activexobject ("scripting. FileSystemObject ")
FSO. createfolder ("2 /")

'Vbs
Set FSO = Createobject ("scripting. FileSystemObject ")
FSO. createfolder ("2 /")

The above two results are the same. After running the command, create a folder named "2.

Objects such as wscript. Shell and wscript. arguments can be called by JS, but JS uses new activexobject and vbs uses set.

Regular Expressions are too complicated, but regular expressions are really powerful!
Originally JavaScript, vbs can also be called. When vbs is called, a new one is used!
Set RegEx = new Regexp
However, JS is more convenient to use, and vbs requires a little trouble.
I am not very skilled in using this item. I will not talk about it here.

However, the default vbs objects, such as wscript, cannot be called by Js.

Also, set, vbs and JS are a little different

'Vbs
STR ="
Set FSO = Createobject ("scripting. FileSystemObject ")
Set DS = FSO. Drives
For each d in DS
STR = STR + D. driveletter + CHR (13)
Next

// JS
VaR STR = ""
VaR FSO = new activexobject ("scripting. FileSystemObject ")
VaR DS = new enumerator (FSO. Drives) // set definition
For (;! DS. atend (); DS. movenext ())
STR = STR + Ds. Item () + "/B"
}
Alert (s)

Aside from the sources and syntax of vbs and JS, vbs and JS are similar in many places. Vbs can do this, and most JS can do this, but some just simulate the effect, a little more trouble. On the contrary, most vbs can do what JS can do.

Embedded in HTML

<Script language = vbsrept>
'Vbs
</SCRIPT>

<SCRIPT langage = JavaScript>
// JS
// You do not need the language attribute or add a version with JavaScript, such as javascript1.2.
// If the JS and vbs of a page are mixed, it is best to add the language attribute.
</SCRIPT>

The on error resume next of vbs is very powerful and convenient for everyone. Especially when you do a webpage, the status bar does not display errors!
In fact, JS works, just window. onerror = return true, which is similar to on error resume next.

In HTML, vbs has the so-called "event-driven", and JS does not.

<Script language = VBScript>
Funtion object_event
End Function
</SCRIPT>

Object_event, that is, adding "_" to an object and events. For example, window_onload indicates that the form is loaded. a_ondblclick indicates that object A is double-clicked. However, you must first define a with the ID.

In addition, JavaScript is more convenient in HTML:

<Body onselectstart = "Return false"> <! -- Disable selection -->
Oncontextmenu = "Return fasle" Disable right-click menu
Oncopy = "Return false" prohibit copying

Using vbs is troublesome. However, there are also some things. It is more convenient to use vbs than to use Js.

Speaking of this, the eval function, JS and vbs both have this function, which is very powerful.

In vbs, Eval is used to check whether an expression is true. The return bool value is trur or false. This function is also available in Js.

Eval also has a function to convert strings into objects (independent vbs won't work)

<! -- JS + HTML -->
<Div id = A3 onclick = CH ()> click Change content </div>
<Script language = JavaScript>
Function CH (){
Eval ("A" + 3). innertext = "A3 object innertext"
}
</SCRIPT>

'Vbs (in HTML)
For ZZ = 0 to document. Links. Length-1
Document. Links (zz). classname = ""
Next
Document. Links (eval (amount). classname = ""

The meaning of this section is to detect all hyperlinks. If the class attribute of the amout hyperlink is not checked once and changed to a, this section is one of the Chinese poems I wrote in HTML, all the hyperlinks except the highlighted ones are in another color.

In addition, Eval can also convert a string into a Timer: setTimeout (), setinterval ()

The following example is also frequently used.

<Span id = A1 onclick = f (this) class = A> 1 </span> & nbsp;
<Span id = a2 onclick = f (this) class = A> 2 </span> & nbsp;
<Span id = A3 onclick = f (this) class = A> 3 </span> & nbsp;
<Span id = A4 onclick = f (this) class = A> 4 </span> & nbsp;
<Span id = A5 onclick = f (this) class = A> 5 </span> & nbsp;

<Style>
. A {color: red; cursor: hand ;}
# A1 {color: Blue ;}
</Style>

<SCRIPT>
Function f (o ){
For (I = 1; I <= 5; I ++ ){
Eval ("A" + I). style. color = "red"
}
O. style. color = "blue"
}
</SCRIPT>

In short, vbs and JS have their own strengths and are used at any convenience!

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.