Java Script                                                    Instructor: Roseann Krane

What is JavaScript?

    It is a scripting language. Programming languages are controlled by a programmer and convert from a human-readable form into a machine-readable form before the program runs by a highly specialized piece of software called a compiler. With a scripting language there is no need to explicitly invoke the code conversion process because it happens automatically in the background when the source code is processed by the target program. A web browser interrupts and dynamically converts the code line by line at run time.

    Scripting languages are compact, easy-to-use program-development languages. A short, simple block of scripting code (JavaScript, for example) can almost do the same thing as a much longer, more complex block of standard programming code. There are tradeoffs, such as power, as scripting languages have less functionality.

    Netscape developed "LiveScript" to enable HTML authors to enhance their pages and provide basic user interaction. Sun joined this effort through their work with the Java programming language and the new product became JavaScript. JavaScript is capable of detecting various conditions in the current operating environment and reacting accordingly.

    JavaScript resembles Java in syntax and use of objects. JavaScript is a distinct program-development language. Both are similar in syntax to C and C++ languages. JavaScript uses built-in objects. Java uses object classes and inheritance. JavaScript code is embedded in HTML page and Java code is in a separate file. JavaScript is interpreted and executed on client machines. Java is compiled on a server and executed on a client computer. Java has strong typing and variables which must be declared and JavaScript has loose typing and variables are not declared.

  • JavaScript supports standard programming constructs including values, names, literals, variables, arrays, operators, expressions, statements, parameters, and control-flow structures.
  • JavaScript supports objects. It has an extensive library of built-in objects: browser windows, frames, links, forms, and form elements. You can also create your own objects, methods, and properties.
  • JavaScript supports functions, and has some built-in data-type conversion functions; parseInt(), for example, translates a numeric string into an integer. JavaScript allows the creation of user defined functions that accept parameters and return values.
  • JavaScript is an event-driven language. An event is a user or system action: a key press, a mouse click, and so on. You program JavaScript to respond to an event by running a specified block of code.
  • JavaScript does NOT:

    Some useful tasks for JavaScript:

    1. Enabling users to interact with HTML pages. JavaScript code can respond directly to user interaction with <FORM> elements (text boxes, buttons, check boxes, drop-down selection lists, and so on), client-side image maps, images, and hypertext links.
    2. Validating data on the client before submitting it to the server.
    3. Managing interactive multi-frame Web pages.
    4. Spicing up Web pages with the date, time, status bar messages, scrolling banners, etc.

    How does it work?

        It is very easy for a Web browser to detect if a HTML page contains embedded JavaScript code. Just use the <SCRIPT> tag to mark the beginning of a JavaScript section and then use the </SCRIPT> tag to indicate the end of that section. Everything between the tags will be interpreted as JavaScript source code rather than standard HTML text. The browser converts the script into its equivalent machine-readable form called binary code and that is executed and the result is inserted into HTML text stream and displayed as if it has been typed into the original HTML document by a programmer.

        You create a JavaScript script by inserting JavaScript code into an HTML document. You can insert a block of code by enclosing it between tags as shown below:

    <SCRIPT LANGUAGE="JavaScript">
    code block ....
    </SCRIPT>

    Or you can embed JavaScript code within an HTML tag. For example, the following bolded JavaScript code is embedded within a tag: 

        <BODY BGCOLOR = "black" TEXT="white" 
        onLoad= "window.defaultStatus='Home Frame'">


    JS#1assignment, part 1: Add the following to your web page:

    <SCRIPT LANGUAGE="JavaScript">
      lastModDate = new Date(document.lastModified)
      document.write("Last updated: " + lastModDate)
    </script>


        What happened? 

    JS#1assignment, part 2:

    Now modify the date script so that it is compact, italicized, and has mm/dd/yy format. 

    Example to insert: lastModMonth = lastModDate.getMonth() + 1 

        and you'll need a line for Date and Year. You can hide the code with commands like this:

        <SCRIPT LANGUAGE="JavaScript">
            <!-- hide JS code
                  ...... something here
            // end JS hide -->
        </script>


    Hopefully you noticed the // preceding the last line to be hidden ending with the -->. The idea is to hide the javascript from browsers that are not capable of utilizing the javascript. But some browsers won't understand the hide marks used in HTML within JavaScript unless you include the // at the beginning of the last hidden marked line.
    End of lesson 1.

    JS#1assignment, part 3:

    Questions (E-mail the answers to the teacher, put "JS1 Questions" in the subject line!):

    1. What is a scripting language?
    2. List ways scripting languages differ from programming languages.
    3. What are some of the main programming features that JavaScript supports?
    4. Name some tasks for which JavaScript is well suited.
    5. What are the two methods you can use to insert JavaScript code into an HTML document?