លំហាត់អនុវត្តន៍ VBScript

លទ្ធផល Output
សរសេរ​អត្ថបទ​ដោយ​ប្រើ VBScript
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>
<script type=”text/vbscript”>
document.write(“Hello World!”)
</script>
</body>
</html>
សរសេរ HTML ដែល​ប្រើ  VBScript
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>
<script type=”text/vbscript”>
document.write(“<h1>Hello World!</h1>”)
</script>
</body>
</html>

អថេរ Variables
បង្កើត​អថេរ​មួយ  variable
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>
<script type=”text/vbscript”>
Dim firstname
firstname=”Hege”
document.write(firstname)
document.write(“<br>”)
firstname=”Tove”
document.write(firstname)
</script>
<p>The script above declares a variable, assigns a value to it, and displays the value. Then, it changes the value, and displays the value again.</p>
</body>
</html>

បញ្ចូល​ជា​តម្លៃ​អថេរ​នៅ​ក្នុង​អត្ថបទ​មួយ  text
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>
<script type=”text/vbscript”>
Dim name
name=”Jan Egil”
document.write(“My name is: ” & name)
</script>
</body>
</html>
បង្កើត​អា​រេ​មួយ array
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>
<script type=”text/vbscript”>
Dim famname(5)
famname(0)=”Jan Egil”
famname(1)=”Tove”
famname(2)=”Hege”
famname(3)=”Stale”
famname(4)=”Kai Jim”
famname(5)=”Borge”
For i=0 To 5
document.write(famname(i) & “<br>”)
Next
</script>

</body>
</html>
Sub procedure
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>

<script type=”text/vbscript”>
Sub mysub()
document.write(“I was written by a sub procedure”)
End Sub
document.write(“I was written by the script<br>”)
Call mysub()
</script>

</body>
</html>
អនុគមន៍ procedure
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>
<script type=”text/vbscript”>
Function myfunction()
myfunction=Date()
End Function
document.write(“Today’s date: “)
document.write(myfunction())
</script>
<p>A Function procedure can return a result.</p>
</body>
</html>

សេចក្តី​ថ្លែង​ការ​ណ៏​ដោយ​មាន​លក្ខខណ្ឌ Conditional Statements
សេចក្តី​ថ្លែង​ការ​ណ៏ If…Then..Else statement
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>

<script type=”text/vbscript”>
i=hour(time)
If i < 10 Then
document.write(“Good morning!”)
Else
document.write(“Have a nice day!”)
End If
</script>

</body>
</html>
សេចក្តី​ថ្លែង​ការ​ណ៏ If…Then..ElseIf statement
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>

<script type=”text/vbscript”>
i=hour(time)
If i = 10 Then
document.write(“Just started…!”)
ElseIf i = 11 Then
document.write(“Hungry!”)
ElseIf i = 12 Then
document.write(“Ah, lunch-time!”)
ElseIf i = 16 Then
document.write(“Time to go home!”)
Else
document.write(“Unknown”)
End If
</script>

</body>
</html>
ជ្រើស​ករណី​សេចក្តី​ថ្លែងការណ៍  Select Case statement
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>

<body>
<script type=”text/vbscript”>
d=weekday(Date)

Select Case d
Case 1
document.write(“Sleepy Sunday”)
Case 2
document.write(“Monday again!”)
Case 3
document.write(“Just Tuesday!”)
Case 4
document.write(“Wednesday!”)
Case 5
document.write(“Thursday…”)
Case 6
document.write(“Finally Friday!”)
Case Else
document.write(“Super Saturday!!!!”)
End Select
</script>

<p>This example demonstrates the “Select Case” statement.</p>
<p>You will receive a different greeting based on what day it is.</p>
<p>Note that Sunday=1, Monday=2, Tuesday=3, etc.</p>

</body>
</html>
តំណ​ចៃដន្យ Random link
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>

<script type=”text/vbscript”>
randomize()
r=rnd()
If r>0.5 Then
document.write(“<a href=’http://www.w3schools.com’>Learn Web Development!</a>”)
Else
document.write(“<a href=’http://www.refsnesdata.no’>Visit Refsnes Data!</a>”)
End If
</script>

<p>
This example demonstrates a link, when you click on the link it will take you to W3Schools.com OR to
RefsnesData.no. There is a 50% chance for each of them.
</p>
</body>
</html>

អនុគមន៍ Looping

រង្វិល​ជុំ​បន្ទាប់ For..Next loop
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>

<script type=”text/vbscript”>
For i = 0 To 5
document.write(“The number is ” & i & “<br>”)
Next
</script>

</body>
</html>
Looping តាម​រយៈ​បឋមកថា  HTML headers
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>

<script type=”text/vbscript”>
For i=1 To 6
document.write(“<h” & i & “>This is header ” & i & “</h” & i & “>”)
Next
</script>

</body>
</html>
ចំពោះ .. រង្វិល​ជុំ​នីមួយ For..Each loop
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>

<script type=”text/vbscript”>
Dim cars(2)
cars(0)=”Volvo”
cars(1)=”Saab”
cars(2)=”BMW”

For Each x In cars
document.write(x & “<br>”)
Next
</script>

</body>
</html>
តើ … ខណៈ​ពេល​ដែល​ការ​រង្វិល​ជុំ Do…While loop
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>

<script type=”text/vbscript”>
i=0
Do While i < 10
document.write(i & “<br>”)
i=i+1
Loop
</script>

</body>
</html>

អនុគមន៍កាលបរិច្ឆេទ​និង​ម៉ោង Date and Time Functions
បង្ហាញ​កាលបរិច្ឆេទ​និង​ពេលវេលា  Date and Time
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>

<script type=”text/vbscript”>
document.write(“Today’s date is ” & Date())
document.write(“<br>”)
document.write(“The time is ” & Time())
</script>

</body>
</html>
បង្ហាញ​ថ្ងៃ​ដែល  days
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>

<p>VBScripts’ function <b>WeekdayName</b> is used to get a weekday:</p>
<script type=”text/vbscript”>
document.write(“<p>”)
document.write(WeekDayName(1))
document.write(“<br>”)
document.write(WeekDayName(2))
document.write(“</p><p>”)

document.write(“Get the abbreviated name of a weekday:”)
document.write(“<br>”)
document.write(WeekDayName(1,True))
document.write(“<br>”)
document.write(WeekDayName(2,True))
document.write(“</p><p>”)

document.write(“Get the current weekday:”)
document.write(“<br>”)
document.write(WeekdayName(weekday(Date)))
document.write(“<br>”)
document.write(WeekdayName(weekday(Date), True))
document.write(“</p>”)
</script>

</body>
</html>

បង្ហាញ​ខែ​នា​ពេល​បច្ចុប្បន្ន​និង​ថ្ងៃ  months
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>

<p>VBScripts’ function <b>MonthName</b> is used to get a month:</p>
<script type=”text/vbscript”>
document.write(“<p>”)
document.write(MonthName(1))
document.write(“<br>”)
document.write(MonthName(2))
document.write(“</p><p>”)

document.write(“Here is how you get the abbreviated name of a month:”)
document.write(“<br>”)
document.write(MonthName(1,True))
document.write(“<br>”)
document.write(MonthName(2,True))
document.write(“</p><p>”)

document.write(“Here is how you get the current month:”)
document.write(“<br>”)
document.write(MonthName(Month(Date)))
document.write(“<br>”)
document.write(MonthName(Month(Date),True))
document.write(“</p>”)
</script>

</body>
</html>
ទៅ Countdown 3000 ឆ្នាំ
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>

<p>Countdown to year 3000:</p>

<p>
<script type=”text/vbscript”>
millennium=CDate(“1/1/3000 00:00:00”)
document.write(“It is ” & DateDiff(“yyyy”, Now(), millennium) & ” years to year 3000!<br>”)
document.write(“It is ” & DateDiff(“m”, Now(), millennium) & ” months to year 3000!<br>”)
document.write(“It is ” & DateDiff(“d”, Now(), millennium) & ” days to year 3000!<br>”)
document.write(“It is ” & DateDiff(“h”, Now(), millennium) & ” hours to year 3000!<br>”)
document.write(“It is ” & DateDiff(“n”, Now(), millennium) & ” minutes to year 3000!<br>”)
document.write(“It is ” & DateDiff(“s”, Now(), millennium) & ” seconds to year 3000!<br>”)
</script>
</p>

</body>
</html>

បន្ថែម​ចន្លោះ​ពេល​មួយ​ទៅ​មួយ​កាល​បរិច្ឆេទ
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>

<script type=”text/vbscript”>
document.write(DateAdd(“d”,30,Date()))
</script>

<p>
This example uses <b>DateAdd</b> to calculate a date 30 days from today.
</p>
<p>
Syntax for DateAdd: DateAdd(interval,number,date).
</p>

</body>
</html>
ទ្រង់ទ្រាយ​កាលបរិច្ឆេទ​និង​ម៉ោង Format Date and Time
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>

<script type=”text/vbscript”>
document.write(FormatDateTime(Date(),vbGeneralDate))
document.write(“<br>”)
document.write(FormatDateTime(Date(),vbLongDate))
document.write(“<br>”)
document.write(FormatDateTime(Date(),vbShortDate))
document.write(“<br>”)
document.write(FormatDateTime(Now(),vbLongTime))
document.write(“<br>”)
document.write(FormatDateTime(Now(),vbShortTime))
</script>

<p>Syntax for FormatDateTime: FormatDateTime(Date,namedformat).</p>

</body>
</html>
តើ​នេះ​ជា​កាល​បរិច្ឆេទ​ឬ​ទេ​?
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>

<script type=”text/vbscript”>
somedate=”10/30/99″
document.write(IsDate(somedate))
</script>

</body>
</html>

អនុគមន៍សាងសង់​ក្នុង​ផ្សេងទៀត  Built-in Functions
អក្សរ​ធំ​ឬ​តួអក្សរ​តូច​ Uppercase or lowercase?
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>

<script type=”text/vbscript”>
txt=”Have a nice day!”
document.write(UCase(txt))
document.write(“<br>”)
document.write(LCase(txt))
</script>

</body>
</html>

យក​ចេញ​ឈាន​មុខ​គេ​ឬ​ដកឃ្លា​នៅ​ពី​ក្រោយ​ចេញ​ពី​ខ្សែ​អក្សរ  string
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>

<script type=”text/vbscript”>
fname=” Bill ”
document.write(“Hello” & Trim(fname) & “Gates<br>”)
document.write(“Hello” & RTrim(fname) & “Gates<br>”)
document.write(“Hello” & LTrim(fname) & “Gates<br>”)
</script>

</body>
</html>

បញ្ច្រាស​ខ្សែ​អក្សរ​មួយ Reverse a string
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>

<script type=”text/vbscript”>
sometext = “Hello Everyone!”
document.write(StrReverse(sometext))
</script>

</body>
</html>
ជុំ​ទី​មួយ​ចំនួន Round a number
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>

<script type=”text/vbscript”>
i = 48.66776677
j = 48.3333333
document.write(Round(i))
document.write(“<br>”)
document.write(Round(j))
</script>

</body>
</html>
ត្រឡប់​មួយ​ចំនួន​ចៃដន្យ Return a random number
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>

<script type=”text/vbscript”>
Randomize()
document.write(Rnd())
</script>

</body>
</html>

ត្រឡប់​មួយ​ចំនួន​ចៃដន្យ​រវាង 0-99 random number between 0-99
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>

<script type=”text/vbscript”>
Randomize()
randomNumber=Int(100 * Rnd())
document.write(“A random number: <b>” & randomNumber & “</b>”)
</script>

</body>
</html>

ត្រឡប់​មួយ​ចំនួន​ដែល​បាន​បញ្ជាក់​ characters from a string
កូដអនុវត្តន៍
<!DOCTYPE html>
<html>
<body>

<script type=”text/vbscript”>
sometext=”Welcome to our Web Site!!”
document.write(Mid(sometext, 9, 2))
</script>

</body>
</html>