Javascript is a language often used within html documents to trigger interactive features.
Since its release in December 4, 1995 (six months after Java, which was released in May 23, 1995), JavaScript has gone through many changes. JavaScript began as a client-side programming language (which runs inside a web browser via a built-in JavaScript engine) for adding interactive contents to the web pages. It became more robust with DHTML (1997) and Ajax (1999). With Node.js (released in May 27, 2009), JavaScript can be used to program server-side and build full-stack web applications. In 2015, the ECMAScript 6 (ES6) introduces major update to the language. Show
IntroductionIt began as a Client-side Programming Language run inside a web browser via a Built-in JavaScript EngineJavaScript is the most widely used client-side programming language that lets you supercharge your HTML with interactivity, animation and dynamic visual effect for better User Interface and User Experience (UI/UX). It is:
JavaScript works together with HTML/CSS. HTML provides the contents; CSS specifies the presentation; and JavaScript programs the behavior. Together, they enrich the UI/UX of the web users. JavaScript is Now Everywhere with Node.jsJavaScript has grown beyond the client-side programming. With the introduction of Node.js in 2009 (an open-source, cross-platform JavaScript run-time environment), you can run your JavaScript standalone or inside the server (instead of a browser). This allows you to use one single language for both the server-side and client-side programming. History and VersionsJavaScript, originally called LiveScript, was created by Brendan Eich at Netscape in 1995. Soon after, Microsoft launched its own version of JavaScript called JScript. Subsequently, Netscape submitted it to ECMA (formerly "European Computer Manufacturers Association", now "Ecma International - European association for standardizing information and communication systems") for standardization, together with Microsoft's JScript. The ECMA Specification is called "ECMA-262 ECMAScript Language Specification" @ http://www.ecma-international.org/publications/standards/Ecma-262.htm (also approved as "ISO/IEC 16262"):
JavaScript vs. JavaJava is a full-fledged general-purpose programming language. It was created by James Gosling at Sun Microsystems (now part of Oracle) and released in August 1995. JavaScript was created by Brendan Eich at Netscape, also in 1995. Originally called LiveScript, it was a small and lightweight special-purpose language for writing client-side program running inside a browser to create active user-interface and generate dynamic web pages. It was renamed to JavaScript in an ill-fated marketing decision to try to capitalize on the popularity of Java language, when Netscape released its Navigator 2 in 1996. Java and JavaScript are totally different languages for different programming purposes. However, in the early days, some efforts were made to adopt Java syntax and convention into JavaScript, such that JavaScript seems to be a subset of Java. In reality, they have very little in common. What Client-side JavaScript CANNOT Do?Remember that JavaScript is a client-side program that you downloaded from a server, and run inside the browser of your (client) machine. What to stop someone from writing a JavaScript that wipes out your hard disk, or triggers a denial-of-service attack to another server? As a result, for security purpose,
jQueryjQuery is a popular cross-browser JavaScript Library. jQuery is JavaScript (but having its own extension syntax), hence, you need to understand JavaScript. I suggest you read through the JavaScript syntax (and pay particular attention to objects), and then switch into jQuery for your production. Read "jQuery Basics". Standalone and Server-side JavaScripts with Node.js JavaScript Engine[TODO] JavaScript Source-Code Editors and IDEsYou need a text editor to write your JavaScript. You could use a plain-text editor such as Windows' NotePad or macOS's TextEdit (strictly NOT recommended as they can't do syntax highlighting). To improve your productivity, a good source-code editor (which provides syntax highlighting, auto-code-complete, snippets, documentation, symbol navigation, refactoring, etc.) is essential. There are many freeware/shareware available, such as Visual Studio Code (VS Code), Sublime Text, Atom, Brackets, Komodo Edit, Emacs, Vim, BBEdit, TextMate, NotePad++ (Windows), etc. You can also use a full-scale IDE such as Eclipse, NetBeans, IntelliJ IDEA. There are also many "online" editors and compilers available (e.g., Programiz, JSFiddle, Playcode), but I think that you should install your own. Client-Side JavaScript by ExamplesI shall assume that you know HTML and CSS (read my HTML/CSS articles otherwise). I shall also assume that you understanding some programming basics (computational thinking) such as variables, if-else and for-loop constructs. Client-side JavaScripts run inside a browser via a built-in JavaScript engine. There are standards on JavaScript. But the Big-5 (Chrome, Firefox, IE/Edge, Safari and Opera), in particular the IE, does not adhere to all the standards strictly. Furthermore, they create their own extensions. Hence, the behavior of JavaScript could be different in different browsers. You may need to test your JavaScripts on more than one browsers. JavaScript also run standalone (and in the server). To run JavaScript standalone, you need to install Node.js, which is a JavaScript engine. I will present the examples in the next section. Client-side JS EG 1: Functions alert() and document.write()Let us write our first client-side JavaScript to print the message "Hello, world". Start with a new file and enter the following codes. Do not enter the line numbers, which is used to aid in explanation. Take note that:
Save the file as "JSExAlertWrite.html" (or any filename that you prefer, with file extension of ".html" or ".htm"). Run the script by loading the HTML file into a JavaScript-capable browser (e.g., One of the BIG FIVE - Chrome, Firefox, Internet Explorer/Edge, Safari or Opera). My first JavaScript says:How it Works?
TRY: Print the document's title and the URL location. (Hints: use document.title and document.location properties.) Don't Get Caught by the Cache - Do Control-Refresh (or Control-F5)If you modify the codes and reload the web page, the new codes may not get executed because the browser caches the previously loaded version. You could use Control-F5 (Control-Refresh) to ask the browser to discard the cache, and fetch a new page. Client-Side JS EG 2: Variables and Functions prompt(), confirm()This script prompts the user for his/her name, confirms the name, and prints a greeting message. There are three kinds of pop-up dialog boxes for interacting with the users:
Welcome to JavaScript! How it Works?
TRY: Instead of printing the greeting message using document.write(), do it via an alert(). Client-Side JS EG 3: The Date ObjectThe following script creates a Date object representing the current date-time, and prints the current time. How it Works?
TRY:
Client-Side JS EG 4: LoopsThe following script prompts the user for a multiplier, and prints the multiples of 1 to 100 using a for-loop. Testing LoopHow it Works?
TRY:
Client-Side JS EG 5: User-defined Functions and onclick Event HandlerBesides the JavaScript built-in functions such as alert(), prompt(), write(), and writeln(), you can define your own functions. A function has a name and a body consisting of a set of JavaScript statements that collectively performs a certain task. It may take zero or more argument(s) from the caller and return zero or one value back to the caller. Example on event and user-defined functionHow it Works?
TRY: Include another button, which opens "JSExVar.html". Client-Side JS EG 6: More Event Handlers: onload, onmouseover and onmouseoutJavaScript can be used to handle many types of events, in response to a user's action or browser's action. For example,
"Hello" alert Box appears after the page is loaded. Point your mouse pointer here!!! Dissecting the Program
Client-Side JS EG 7: Separating HTML, CSS and JavaScriptThe previous example works fine. You will find many such example in textbooks, especially the older textbooks. However, it has a big problem. All the HTML contents, CSS presentation styles and JavaScript programming codes are placed in a single file. For a small toy program, the problem is not serious. But when your program grows and if the HTML, CSS and JavaScript are written by different people, you will have a real challenge in maintaining the program. Remember that HTML is for the contents, CSS for presentation and JavaScript for the behavior. Let's rewrite the example to place the HTML, CSS and JavaScript in three different files. "Hello" alert Box appears after the page is loaded. Point your mouse pointer here!!! /* JSExFiles.css */ @CHARSET "UTF-8"; .highlight { color: red; }// JSExFiles.js window.onload = function() { init(); alert("Hello!"); } function init() { document.getElementById("magic").onmouseover = function() { this.className = "highlight"; } document.getElementById("magic").onmouseout = function() { this.className = ""; } }How it Works?
|