Development of Internet Application 1501CT - Sara Almudauh VBScript Part 1 Lecture 6 Sara Almudauh Development of Internet Application 1501CT - Sara Almudauh
Development of Internet Application 1501CT - Sara Almudauh VBScript is a Microsoft scripting language. VBScript is a light version of Microsoft's programming language Visual Basic. VBScript is the default scripting language in ASP (Active Server Pages). Development of Internet Application 1501CT - Sara Almudauh
Development of Internet Application 1501CT - Sara Almudauh VBScript on a Server When VBScript is used with ASP, the statement response.write() produces output. <html> <body> <%response.write("This is my first VBScript")%> </body> </html> Development of Internet Application 1501CT - Sara Almudauh
Development of Internet Application 1501CT - Sara Almudauh VBScript Variables A variable can have a short name, like x, or a more descriptive name, like carname. Rules for VBScript variable names: Must begin with a letter Cannot contain a period (.) Cannot exceed 255 characters In VBScript, all variables are of type variant, that can store different types of data. Development of Internet Application 1501CT - Sara Almudauh
Declaring (Creating) VBScript Variables You can declare VBScript variables with the Dim, Public or the Private statement. Like this: Dim x Dim carname Development of Internet Application 1501CT - Sara Almudauh
Development of Internet Application 1501CT - Sara Almudauh Example <!DOCTYPE html> <html> <body> <% Dim firstname firstname=“Sara" response.write(firstname) response.write("<br>") firstname=“Ahmed" response.write(firstname) %> </body> </html> Sara Ahmed Development of Internet Application 1501CT - Sara Almudauh
Development of Internet Application 1501CT - Sara Almudauh Example <!DOCTYPE html> <html> <body> <% Dim firstname firstname=“Sara” response.write(“your first name is :”&firstname) %> </body> </html> your first name is : Sara Development of Internet Application 1501CT - Sara Almudauh
Development of Internet Application 1501CT - Sara Almudauh VBScript Procedures VBScript has two kinds procedures: Sub procedure Function procedure Development of Internet Application 1501CT - Sara Almudauh
Development of Internet Application 1501CT - Sara Almudauh Sub Procedures A Sub procedure: is a series of statements, enclosed by the Sub and End Sub statements can perform actions, but does not return a value can take arguments Sub mysub() some statements End Sub Sub mysub(argument1,argument2) some statements End Sub or Development of Internet Application 1501CT - Sara Almudauh
Development of Internet Application 1501CT - Sara Almudauh Example <!DOCTYPE html> <html> <body> <% Sub mysub() response.write("I was written by a sub procedure") End Sub response.write("I was written by the script<br>") Call mysub() %> </body> </html> I was written by the script I was written by a sub procedure Development of Internet Application 1501CT - Sara Almudauh
VBScript Function Procedures A Function procedure: is a series of statements, enclosed by the Function and End Function statements can perform actions and can return a value can take arguments that are passed to it by a calling procedure without arguments, must include an empty set of parentheses () returns a value by assigning a value to its name Development of Internet Application 1501CT - Sara Almudauh
VBScript Function Procedures Function myfunction() some statements myfunction=some value End Function or Function myfunction(argument1,argument2) some statements myfunction=some value End Function Development of Internet Application 1501CT - Sara Almudauh
Development of Internet Application 1501CT - Sara Almudauh Example <!DOCTYPE html> <html> <body> <% Function myfunction() myfunction=Date() End Function response.write("Today's date: ") response.write(myfunction()) %> <p>A Function procedure can return a result.</p> </body> </html> Today's date: 2/22/2016 A Function procedure can return a result. Development of Internet Application 1501CT - Sara Almudauh
Development of Internet Application 1501CT - Sara Almudauh Calling a Procedure <!DOCTYPE html> <html> <body> <% Function myfunction(a,b) myfunction=a+b End Function response.write(myfunction(5,9)) %> </body> </html> 14 Development of Internet Application 1501CT - Sara Almudauh
Development of Internet Application 1501CT - Sara Almudauh Calling a Procedure When you call a procedure you can use the Call statement, like this: Call MyProc(argument) Or, you can omit the Call statement, like this: MyProc argument Development of Internet Application 1501CT - Sara Almudauh
VBScript Conditional Statements In VBScript we have four conditional statements: If statement - executes a set of code when a condition is true If...Then...Else statement - select one of two sets of lines to execute If...Then...ElseIf statement - select one of many sets of lines to execute Select Case statement - select one of many sets of lines to execute Development of Internet Application 1501CT - Sara Almudauh
VBScript Conditional Statements If you want to execute only one statement when a condition is true, you can write the code on one line: If i=10 Then response.write("Hello") If you want to execute more than one statement when a condition is true, you must put each statement on separate lines, and end the statement with the keyword "End If": If i=10 Then response.write("Hello") i = i+1 End If Development of Internet Application 1501CT - Sara Almudauh
VBScript Conditional Statements There is no ..Else.. in the example above either. You just tell the code to perform multiple actions if the condition is true. If you want to execute a statement if a condition is true and execute another statement if the condition is not true, you must add the "Else" keyword: <!DOCTYPE html> <html> <body> <% i=hour(time) If i < 10 Then response.write("Good morning!") Else response.write("Have a nice day!") End If %> </body> </html> Good morning! Development of Internet Application 1501CT - Sara Almudauh
VBScript Conditional Statements If...Then...ElseIf You can use the If...Then...ElseIf statement if you want to select one of many blocks of code to execute: <html> <body> <% i=hour(time) If i = 10 Then response.write("Just started...!") ElseIf i = 11 Then response.write("Hungry!") ElseIf i = 12 Then response.write("Ah, lunch-time!") ElseIf i = 16 Then response.write("Time to go home!") Else response.write("Unknown") End If %> </body> </html> Good morning! Development of Internet Application 1501CT - Sara Almudauh
Development of Internet Application 1501CT - Sara Almudauh Select Case You can also use the "Select Case" statement if you want to select one of many blocks of code to execute: d=weekday(Date) Select Case d Case 1 response.write("Sleepy Sunday") Case 2 response.write("Monday again!") Case 3 response.write("Just Tuesday!") Case 4 response.write("Wednesday!") Case 5 response.write("Thursday...") Case 6 response.write("Finally Friday!") Case Else response.write("Super Saturday!!!!") End Select %> Development of Internet Application 1501CT - Sara Almudauh
Development of Internet Application 1501CT - Sara Almudauh VBScript Looping In VBScript we have four looping statements: For...Next statement - runs code a specified number of times For Each...Next statement - runs code for each item in a collection or each element of an array Do...Loop statement - loops while or until a condition is true While...Wend statement - Do not use it - use the Do...Loop statement instead Development of Internet Application 1501CT - Sara Almudauh
Development of Internet Application 1501CT - Sara Almudauh For...Next Loop Use the For...Next statement to run a block of code a specified number of times. The For statement specifies the counter variable (i), and its start and end values. The Next statement increases the counter variable (i) by one. <html> <body> <% For i = 0 To 5 response.write("The number is " & i & "<br />") Next %> </body> </html> The number is 0 The number is 1 The number is 2 The number is 3 The number is 4 The number is 5 Development of Internet Application 1501CT - Sara Almudauh
Development of Internet Application 1501CT - Sara Almudauh Do...Loop If you don't know how many repetitions you want, use a Do...Loop statement. The Do...Loop statement repeats a block of code while a condition is true, or until a condition becomes true. Repeat Code While a Condition is True You use the While keyword to check a condition in a Do...Loop statement. Do While i>10 some code Loop Development of Internet Application 1501CT - Sara Almudauh
Read more about Loops in VB http://www.w3schools.com/asp/vbscript_looping.asp http://www.tutorialspoint.com/vbscript/ http://www.pickatutorial.com/tutorial/vbscript/first_vbscript_code.htm Development of Internet Application 1501CT - Sara Almudauh
Development of Internet Application 1501CT - Sara Almudauh VBScript Examples Development of Internet Application 1501CT - Sara Almudauh
Development of Internet Application 1501CT - Sara Almudauh Display Date and Time <!DOCTYPE html> <html> <body> <% response.write("Today's date is " & Date()) response.write("<br>") response.write("The time is " & Time()) %> </body> </html> Today's date is 2/22/2016 The time is 2:30:17 PM Development of Internet Application 1501CT - Sara Almudauh
Development of Internet Application 1501CT - Sara Almudauh Display Days <!DOCTYPE html> <html> <body> <p>VBScripts' function <b>WeekdayName</b> is used to get a weekday:</p> <% response.write("<p>") response.write(WeekDayName(1)) response.write("<br>") response.write(WeekDayName(2)) response.write("</p><p>") response.write("Get the abbreviated name of a weekday:") response.write("<br>") response.write(WeekDayName(1,True)) response.write("<br>") response.write(WeekDayName(2,True)) response.write("</p><p>") response.write("Get the current weekday:") response.write("<br>") response.write(WeekdayName(weekday(Date))) response.write("<br>") response.write(WeekdayName(weekday(Date), True)) response.write("</p>") %> </body> </html> VBScripts' function WeekdayName is used to get a weekday: Sunday Monday Get the abbreviated name of a weekday: Sun Mon Get the current weekday: Monday Mon Development of Internet Application 1501CT - Sara Almudauh
Display the current month and day <!DOCTYPE html> <html> <body> <% response.write("Today's day is " & WeekdayName(Weekday(Date))) response.write("<br>") response.write("The month is " & MonthName(Month(Date))) %> </body> </html> Today's day is Monday The month is February Development of Internet Application 1501CT - Sara Almudauh
Development of Internet Application 1501CT - Sara Almudauh More Examples http://www.w3schools.com/asp/vbscript_examples.asp Development of Internet Application 1501CT - Sara Almudauh
Development of Internet Application 1501CT - Sara Almudauh Questions !! Development of Internet Application 1501CT - Sara Almudauh