JavaScript
  Home arrow JavaScript arrow Page 3 - The Power of Javascript: Controlling the E...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

The Power of Javascript: Controlling the Execution of the Script
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 8
    2005-08-09

    Table of Contents:
  • The Power of Javascript: Controlling the Execution of the Script
  • The if statement
  • The if statement example
  • Advanced if statements (else if and else)
  • The switch statement
  • The switch example

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    The Power of Javascript: Controlling the Execution of the Script - The if statement example


    (Page 3 of 6 )

    Copy the following code into an HTML document and save it.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
      <head>
        <title>Hello World</title>
        <script language="JavaScript" type="text/javascript">
          var someText;
          someText = prompt("Please enter some text", "I like Javascript");
          if(someText == "I like Javascript")
            alert("You have entered the default text");
          document.write("This is the first statement after the if statement"); 
        </script>
      </head>
      <body>
      </body>
    </html>

    When you load the document into your Web browser the dialog box prompt will be displayed as in the previous example:

    Don't change the default text and click on OK, a message box is displayed to tell you that you have entered the default text.

    And the following line will be printed to the Web page:

    Note that this line is written to the Web page whether the if statement's boolean expression evaluated to true or false, because in both cases the interpreter continued the execution of the first statement that followed the if statement. The boolean expression evaluates to true because the value "I like Javascript" is the same value of the variable someText, so that's why the message box is displayed. The document.write() line is not part of the if structure, but intentionally I have done that to explain how important it is to put the curly braces {} with the if statement (and to indent the blocks) so your code becomes more readable. It's better to write the example as follows:

    <script language="JavaScript" type="text/javascript">
      var someText;
      someText = prompt("Please enter some text", "I like Javascript");
      if(someText == "I like Javascript")
      {
        alert("You have entered the default text");
      }
      document.write("This is the first statement after the if statement");
    </script>

    More JavaScript Articles
    More By Michael Youssef


       · I'm very novice in JavaScript, but wouldn't it be possible to create a large web...
     

    JAVASCRIPT ARTICLES

    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway