[Contents] <Lesson 2>
1.1 What is JavaScript?
JavaScript is a scripting language that works with HTML to enhance web pages and make them more interactive by accessing and modifying the content in a webpage. What is a scripting language? A script is made up of a sequence of statements that give instructions for the computer to perform certain tasks, for examples, like providing a response to the users, to play a song, to start a slideshow, to display certain advertisements and so on.
JavaScript can turn a web page into a lively interactive platform for the world wide web users! JavaScript is a scripting language for HTML. Besides that, you can add sound, date, time, change the color of the web page according to a certain day, prevalidate data entered into a form by the user before it is sent to the server, search through a database, set options based on users preferences and much
more.Basically, JavaScript can access the content of a webpage by selecting any element. attribute or text from an HTML page. It can also modify the content of a webpage by adding elements, attributes, and text to the page.
1.2 Why Use JavaScript?
Why use JavaScript? You may have this question in mind. Well, let me get to the point. HTML or HyperText Markup Language was the only language that was used to present text and graphics as well as links to the world wide web users in the 90’s of the last century. Although it was a vast improvement from the earlier text-only browsers like Gopher, it was relatively passive and static, it cannot interact much with the user. That is why we need JavaScript to make browsing the web a more interesting and useful experience for the Internet users.
1.3 The Building Block of JavaScript
As JavaScript is an object-oriented programming language; therefore building block of the JavaScript program code is made up of objects as well as methods, properties, and events associated with the objects.
1.3.1 Objects
The purpose of a JavaScript program is to manipulate the elements of a web page, such as documents, forms, radio buttons, checkboxes, buttons, windows and more. All the aforementioned elements are known as objects.
We need to identify each object in a web page by given a name to it. For example, the default name of a text box is Textbox1; if we insert another text box, the default name will be TextBox2. We can also change the names of the objects so that it is easier for us to identify them, like Txt_StudentName instead of just TextBox1.
1.3.2 Properties
Every object of a web page has many properties. The properties of an object reflect its characteristics or its behaviors. For example, the properties of a text box are name, height, width, background color, with a border or no border, font type and size and more.
1.3.3 Methods
A method is a task carried out by an object. For example, to display a phrase on a web page, we can use the write method, using the syntax document.write(‘Phrase”). For example, document.write(“Welcome to JavaScript”) will display the “Welcome to JavaScript “ message.
1.3.4 Events
An event is an execution of the JavaScript code triggered by an action from the user via the keyboard or mouse. For example, clicking the submit button on a web page will trigger an event that validates the input data and transmits the data to the server. Examples of events are Click, Load, KeyDown, KeyPress, DblClick (Double Click), Select and more.
To respond to the aforementioned events, JavaScript uses event handlers. Some of the common event handlers are: OnClick,OnChange, OnLoad, onSubmit, OnSelect, OnMouseOver,KeyPress
We will learn how to write codes for the event handlers in later lessons.