Advanced ASP programming (3): Accessing script programs

Source: Internet
Author: User

With the enhanced knowledge of form in the previous html, is it possible to start the ASP journey? Not necessarily.
Programmers who have systematically studied ASP will understand that it is always crucial to learn the scripting language before the system learns the embedded objects and components of ASP.
Why? Why should we learn the scripting language? What is the relationship between ASP and scripting?

First, let's talk about why ASP is expensive. ASP is short for Microsoft Active Server Pages. It is a Server-side scripting environment that can be used to create interactive Web Pages and build powerful Web applications. It can be indicated that ASP is a server-based Script environment, so you can understand why ASP requires IIS support, because IIS is a common web server; then you can understand why you want to learn the scripting language, because ASP provides a script environment.

Once again, it should be noted that ASP is only an environment, not a language. To visualize ASP as a network programming languageBuilt-in objects and components provided by html + script + aspPowerful hybrid language.

Therefore, learning scripts is very important! In fact, whether it is a scripting language or other languages, I think the first thing we need to know is the various features covered by the language, including:Data Type, event, method, object, attribute, syntax, etc.Of course, this is a bit of empty talk. Just like a computer language course in a university class, purely research-based learning is a bit boring. The program that can solve the problem is a good program (the cat that can catch the mouse is a classic swordsmanship of the good cat, haha)

Of course, you still need to study the language. For example, if you write a program that calculates the absolute value, an abs () function is actually done. For example, if you use a vb script but replace the same program with a java Script, an error will occur. Their data types are different and the statement syntax is different. Just like Chinese and foreign languages, we need to publish a phonetic alphabet (data type), how to pronounce it (method), how to say it as a sentence (syntax )...... Well, it makes you feel a little uncomfortable. OK! Directly. You need to pay attention to the functions and syntax of the language in the future. Of course, it is not for you to take a full look. Follow the instructions in the tutorial to master one. After completing the tutorial, it's almost over. You have to trust me.

Currently, there are two main scripting languages: VbScript and JavaScript. Because VbScript is the default server script for IIS, we have basically learned about ASP, which uses VbScript as the script. Of course, javascript is not ruled out in some cases.

Here is a simple example:

<Script language = "vbscript"> <br/> <! -- <Br/> sub button1_onclick <br/> msgbox ("Welcome to vbscript! ") <Br/> end sub <br/> --> <br/> </script> </p> <script language = "javascript"> <br/> <! -- <Br/> function b213 () <br/>{< br/> alert ("welcome to use javascript! ") </P> <p >}< br/> --> <br/> </script> <br/> <form> <br/> <input type = "button" name = "button1" value = "vbs"> <br/> <input type = "button" name = "button2" value = "js" onclick = "b213 () "> <br/> </form> <br/> Sub in vbscript is an event process. The process NAME consists of two parts: one part is the button NAME, that is, Button1 (obtained from the NAME attribute in the <INPUT> tag), and the other part is the event NAME, that is, OnClick. The two parts are connected by underscores. When you click the button, Internet Explorer searches for and runs the corresponding event process, that is, Button1_OnClick, and MsgBox is a function. For more specific parameters, you can see it. <Br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]

For a moment, everyone may see a crazy dialog box group on the Internet. What is the principle? See:

<Script language = "vbscript"> <br/> msgbox "you know you will click OK ....... "<Br/> msgbox" does not need to be clicked ........ "<Br/> msgbox", right ..........? "<Br/> msgbox" or the webpage cannot be opened ...... "<Br/> msgbox" can damage people ......... "<Br/> msgbox" really wants to beat the webmaster ......... "<Br/> </script> <br/> In fact, when the webmaster plays with others, he first plays with himself: this is because he has come up with this: <br/> well, I didn't mean anything to everyone, just to learn. <Br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]

How can I create a dialog box on the page or on the page?
<Script language = "vbscript"> <br/> <! -- <Br/> sub window_onload () <br/> msgbox "welcome you! "<Br/> end sub <br/> sub window_onunload () <br/> msgbox" see you late! "<Br/> end sub <br/> --> <br/> </script> <br/> the page displays" welcome you !", "See you late!" is displayed on the off page !" <Br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]

The above is only oneShow dialog box msgboxAnd, of courseWriting dialog box inputboxTake a look.

<Script language = "vbscript"> <br/> <! -- <Br/> dim strname <br/> strname = inputbox ("enter your name please:", "enter your name", "cnbruce") <br/> document. write (strname & ", welcome you! <P> ") <br/> --> <br/> </script> <br/> take a look at the parameters for inputbox. <Br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]

Let's take a look at several practical functions:
Len (): returns the length of the string or the length of the variable in bytes.
Lef (): extract the first part of a string.
Right (): truncates the content of the back part of a string.
<Script language = vbs> <br/> text = "1234567890 abcdefgh" <br/> I = 10 </p> <p> lentext = len (text) <br/> lefttext = left (text, I) <br/> righttext = right (text, I) </p> <p> alert ("the text length is: "& lentext) <br/> alert (" retrieve text from the left "& I &": "& lefttext) <br/> alert ("right-click the text" & I & ":" & righttext) <br/> </script> <br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]

According to the above, we often see the situation of adding "..." after a long article is as follows:

<Script language = vbs> <br/> text = "123 4567 8fds90 abcde fghxcfv" <br/> I = 10 <br/> if len (text)> I then' if the text length is greater than the given value <br/> text = left (text, I) 'extract the I-bit string from the front end <br/> alert (text &"... ") <br/> else <br/> alert (text) <br/> end if <br/> </script> <br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]

There is another importantFunction replace ()
Searches for strings to replace the specified strings.
Replace (strtobesearched, strsearchfor, strreplacewith)
Strtobesearched is a string; strsearchfor is a substring to be searched; strreplacewith is a substring to replace.
The following is a useful text Conversion Program.
<Script language = vbs> <br/> text = "fuck, fuck" </p> <p> text = replace (text, "Fuck", "MD ") <br/> text = replace (text, "", "CAO") <br/> alert (text) </p> </script> <br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]

When sending emails, you often add multiple addresses separated by the ";" sign. How can we break down multiple accepted EMAIL addresses? Use the Split function: the Split function returns an array from a string.
<Script language = vbs> <br/> cnbruce = "cnbruce@126.com; cnbruce@it365cn.com; root@cnbruce.com" <br/> MyArray = Split (cnbruce ,";") <br/> a = MyArray (0) <br/> B = MyArray (1) <br/> c = MyArray (2) <br/> alert () <br/> alert (B) <br/> alert (c) <br/> </script> <br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]

These are more realistic and interesting, and it is good to study in middle school :) but after all, we want to program. When it comes to programming, I know the program structure:
1. Ordered Structure:It is the most common program that runs from top to bottom and from left to right.
2. Select the structure:Here are two conditions for you to choose from: otherwise (if... then), or else (else :)
3. Loop Structure:It gives you the maximum space to play, and activities (loops) are not beyond this range. Otherwise, the system automatically exits (jumps out of the loop ).
Complex programs are simply applied by the comprehensive nesting of these three structures.

There is not much to say about the sequence structure, because the execution of the program is basically in this direction.
Let's take a look at the selection structure. Of course, the selection structure can also be nested.

<Body> <br/> hello, <br/> <script language = "vbscript"> <br/> <! -- <Br/> dim thishour <br/> thishour = hour (time) <br/> if thishour <= 12 then <br/> document. bgcolor = "red" <br/> document. fgcolor = "black" <br/> document. write ("good morning! ") <Br/> else <br/> if thishour <= 18 then <br/> document. bgcolor = "blue" <br/> document. fgcolor = "white" <br/> document. write ("Good afternoon! ") <Br/> else <br/> document. bgcolor = "green" <br/> document. fgcolor = "yellow" <br/> document. write ("Good evening! ") <Br/> end if <br/> --> <br/> </script> <br/> This page displays different colors at different times. and welcome information. <Br/> </body> <br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]

The above program is very simple, as long as you can understand English, you will understand the program (I understand it so :)
First, submit the current hour: hour (time );
Then compare it with 12. If <= 12, it must be in the morning; otherwise, it is in the afternoon and evening;
Otherwise, the condition is further determined. If the time is less than or equal to 18, it must be afternoon;
Finally, needless to say, the blind man guessed it was night too :)

Document. bgcolor in the previous program is the background color of the document, document. fgcolor is the foreground color (text color) of the document, and the next program dynamically changes the background color.

<Script language = "vbscript"> <br/> <! -- <Br/> sub setbgcolor (bcolor) <br/> document. bgcolor = bcolor <br/> end sub <br/> --> <br/> </script> <br/> <form> <br/> <input type = "radio "name =" color "onclick = setbgcolor (" red ")> red <br> <br/> <input type = "radio" name = "color" onclick = setbgcolor ("green")> green <br> <br/> <input type = "radio" name = "color" onclick = setbgcolor ("blue")> blue <br> <br/> <input type = "radio" name = "color" onclick = setbgcolor ("yellow")> yellow <br> <br/> <input type = "radio" name = "color" onclick = setbgcolor ("gray")> gray <br> <br/> </form> <br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]

RelatedNested Condition Selection StructureShow a form detection program.

<Html> <pead> <title> abc </title> <br/> <script language = "vbscript"> <br/> <! -- <Br/> sub btnsubmit_onclick <br/> if form1.name. value <> "" then <br/> if form1.addr. value <> "" then <br/> if form1.email. value <> "" then <br/> if instr (form1.email. value, "@") <> 0 and instr (form1.email. value ,". ") <> 0 then <br/> form1.submit <br/> else <br/> alert" email error! "<Br/> end if <br/> else <br/> alert" enter your email! "<Br/> form1.elements (" email "). focus <br/> end if <br/> else <br/> alert" enter your address! "<Br/> form1.elements (" addr "). focus <br/> end if <br/> else <br/> msgbox" enter your name please! "<Br/> form1.elements (" name "). focus <br/> end if <br/> end sub <br/> --> <br/> </script> <br/> </pead> <br/> <body> <br/> <form name = "form1" method = post action = "bug.html"> <br/> your name: <input type = "text" name = "name"> <br/> your addr: <input type = "text" name = "addr"> <br/> your email: <input type = "text" name = "email"> <br/> <input type = "button" name = "btnsubmit" value = "submit"> <br /> </form> <br/> </body> <br/> </ptml> <br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]

The program is provided, but it seems difficult to understand. Sometimes the execution time of the program is also important, so you have to streamline the program code.
The so-called: easy to write programs, difficult to write classical programs, then the above programs can also change the way of thinking. Use javascript instead)

<Html> <pead> <title> abc </title> <br/> <script language = "javascript"> <br/> <! -- <Br/> function form1_onsubmit () <br/>{< br/> if (document. form1.name. value = "") <br/>{< br/> alert ("set your login name. ") <Br/> document. form1.name. focus () <br/> return false <br/>}< br/> else if (document. form1.addr. value = "") <br/>{< br/> alert ("Enter your address. ") <Br/> document. form1.addr. focus () <br/> return false <br/>}< br/> else if (document. form1.email. value = "") <br/>{< br/> alert ("Enter your email address. ") <Br/> document. form1.email. focus () <br/> return false <br/>}< br/> --> <br/> </script> <br/> </pead> <br /> <body> <br/> <form name = "form1" onsubmit = "return form1_onsubmit () "> <br/> your name: <input type =" text "name =" name "> <br/> your addr: <input type = "text" name = "addr"> <br/> your email: <input type = "text" name = "email"> <br/> <input type = "submit" name = "submit" value = "submit"> <br /> </form> <br/> </body> <br/> </ptml> <br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]

Finally, let's take a look.Loop StructureRight: 1 to 500 is not written one by one.

<Script language = vbs> <br/> for I = 1 to 500 <br/> document. write (I & "<br>") <br/> next <br/> </script> <br/>
[Ctrl + A select all for copy prompt: you can modify some code before clicking run]

Of course, loop can be used not only for, but also do while... loop.
In any case, the program replaces us with a lot of repetitive and boring things-as long as you make rational use of the program.

There should be some achievements. Learning Language and programming means learning syntax and semantics and learning programming architecture ideas.
Of course, this requires you to have a solid foundation of the language. What is the basis? What do you know?Function? Do you know how to use the program to determine the even number (includingOperation)? Do you know how to perform form detection? Do you know the three program architectures?

Function reference:
Http://www.cnbruce.com/blog/showlog.asp? Cat_id = 26 & log_id = 245

Haha, I have thoroughly understood the above programs. I 'd better look at the script reference manual and start ASP with me.

Supplement:

The Function process is similar to the Sub process, but the Function process can return values.
The Function process can also use constants, variables, or expressions passed by the call process as parameters. If the Function process has no parameters, the Function statement must contain parentheses ().
The Function process returns a value through the Function name, which is assigned to the Function name in the statement of the process. The data type returned by Function is always Variant.

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.