Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Your First Script

Your first script is now complete and needs only to be assembled into a single document. Listing 3.3 shows you the complete source code.

Listing 3.3 The Final Script

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html 
   PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
 <meta http-equiv="Content-Script-Type" content="text/javascript" />
 <title>My First JavaScript Script</title>
 <script type="text/javascript" language="JavaScript">
 <!--
  function passCheck(){
   if(prompt("Please enter your password","") == "letmein"){
    window.location = "/secure.html";
   }else{
    window.location = "/error.html";
   }
  }
 //-->
 </script>
</head>
<body onload="passCheck()">
 <noscript>
  <p>
   <b>
   Sorry, but your browser either does not support JavaScript or you
   have it turned off. For information on JavaScript supporting browsers
   or how to turn it back on, please see the website of your browser's
   creator.
   </b>
  </p>
 </noscript>
</body>
</html>

In this listing, you will see that we specified the default language type and our function in the <head> of the page, and we called the function using the onLoad event handler in the <body> element. Finally, we accommodated non-supporting browsers using the <noscript> element in the <body> portion of the page. To see the result of running this script in a JavaScript-enabled browser, see Figure 3.5. Figure 3.6 shows what users of non-supporting browsers (with JavaScript turned off) see.

Figure 3.5 Running Listing 3.3 in a supported browser.

Figure 3.6 Running Listing 3.3 in a non-supported browser.

  • + Share This
  • 🔖 Save To Your Account