Visual Basic
Active Server Pages
Java
Click Here


Home

What's New

Articles

Code Downloads

Code Snippets

Message boards

Links
Tool Box

Books

Mailing List
Receive free code snippets and notices when this site is updated.

Contact

Web Scripting Code Snippets

By Language

 VB Script

Fading Background

Fades background color in from black to white

<script language="VBScript">
<!--
For i = 16 To 255 Step 1
x = Hex(i)
Document.BGCOLOR = x & x & x
Next
// -->
</script>

Displaying how long a user as been on a page 

<script language="VBScript">
<!--
Dim Time1
Sub window_onLoad()
MyTime = Time ' Assign a time.
Time1 = (Minute(MyTime) * 60) + Second(MyTime) + (Hour(MyTime) * 60 * 60)
end sub

Sub window_onUnload()
MyTime = Time ' Assign a time.
Time2= (Minute(MyTime) * 60) + Second(MyTime) + (Hour(MyTime) * 60 * 60)
MsgBox ("You have been here for " & Time2 - Time1 & " Seconds")
end sub
-->
</script> 

The variable "Time1" holds the time from when you loaded the page. Time2 is the time when you leave the page.

In both variables hour's and minute's first have to be converted to seconds. Then the current time is subtracted from the time when you loaded the page.


Personal Greeting

This script displays an inputbox for the user to type in his or her name and then prints a greeting using their name.

<SCRIPT LANGUAGE="VBScript">
<!--

Message = "Hello whats your name" ' Sets prompt.
Title = "Greetings" ' Sets title.
Default = "Web surfer" ' Set default.
' Display message, title, and default value.
Surfer = InputBox(Message, Title, Default)

Document.Write "<h2>Hello " & Surfer & " thanks for visiting </h2>"

-->
</SCRIPT>

Sending Internet Explorer users to a different page.

When you load this page and have a browser capable of reading VBScript it takes you to a page created for Internet Explorer .

<script language="VBScript">
location.href="IeExplorer.html"
</script>

Creating a link VBScript enabled browsers only.

This script is useful if you want to create links only for Microsoft Internet Explorer. As it is the currently the only browser that natively supports VBScript and ActiveX.

<SCRIPT LANGUAGE="VBScript">
<!----
document.write("<a href=activexpage.htm> Click here to go to the ActiveX page</a>")
-->
</SCRIPT>

Java Script

Showing the last modified date

This script prints out the date and time that the document was last modified.

<SCRIPT LANGUAGE="JavaScript">
document.write('<H2>Last modified on ' + document.lastModified + '</H2>');
</SCRIPT>

Personal Greeting

This script displays an inputbox for the user to type in his or her name and then prints a greeting using their name.

<SCRIPT>
var temp="Input your name";
var user_name=prompt(temp,"web surfer");
if (user_name)
document.write("<h2>Hello " + user_name + "! Thanks for visiting!</h2>");
else
document.write("<h2>Hello and thanks for visiting!</h2>");
</SCRIPT>

24 Hour Clock

This script displays a 24 hour clock in a text box.

<HTML>
   <HEAD>
      <TITLE>24 Hour Clock</TITLE>
      <SCRIPT LANGUAGE="JavaScript">
      function setClock() {
           now = new Date();

           var CurHour = now.getHours();
           var CurMinute = now.getMinutes();
           now = null;

           Time= CurHour + ":" + CurMinute 
           document.clocktext.clock.value = Time

           setTimeout ("setClock()", 1000 * 60)
      }
      </SCRIPT>
  </HEAD>
  <BODY onLoad = "setClock()">
      <FORM NAME="clocktext">
         <INPUT TYPE="text" NAME="clock" VALUE="" SIZE=8>
      </FORM>
  </BODY>

</HTML>

12 Hour Clock

This script displays a 12 hour clock in a text box.

<HTML>
  <HEAD>
      <TITLE>12 Hour Clock</TITLE>
      <SCRIPT LANGUAGE="JavaScript">
      function setClock() {
         now = new Date();
         var CurHour = now.getHours();
         var CurMinute = now.getMinutes();
         now = null;
         if (CurHour >= 12) {
           CurHour = CurHour - 12;
           Ampm = "pm";
         }
         else
           Ampm = "am";
           if (CurHour == 0)
              CurHour = "12"
              CurHour = ""+CurHour;
              Time= CurHour + ":" + CurMinute + Ampm
              document.clocktext.clock.value = Time
              setTimeout ("setClock()", 1000 * 60)
      }
      </SCRIPT>
  </HEAD>
  <BODY onLoad = "setClock()">
    <FORM NAME="clocktext">
       <INPUT TYPE="text" NAME="clock" VALUE="" SIZE=8>
    </FORM>
  </BODY>

</HTML>