官术网_书友最值得收藏!

Chapter 1. JavaScript Basics

JavaScript, which was introduced as LiveScript by Netscape Communications Corp, has grown leaps and bounds in the last few years. JavaScript was originally developed to make web pages more interactive, and control the behavior of the page. JavaScript programs are commonly embedded inside an HTML file. HTML is a markup language, and does not manipulate the behavior of a page once its loaded. Using JavaScript, web developers can set rules and verify if the rules were followed, avoiding any remote server resources for input validation or complex number crunching. Today JavaScript is not just used for basic input validation; it is used to access the browser's Document object, to make asynchronous calls to the web server, and to develop end-to-end web applications using software platforms such as Node.JS, which is powered by Google's v8 JavaScript engine.

JavaScript is considered to be one of the three building blocks that are required to create interactive web pages; it is the only programming language in the trinity that is HTML, CSS, and JavaScript. JavaScript is a case sensitive and a space insensitive language, unlike Python and Ruby. A JavaScript program is a collection of statements and those statements have to be included inside the <script>> tags.

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

JavaScript has to be invoked from another application such as a browser. Browsers have a built-in JavaScript engine that interprets and executes the JavaScript on the webpage. The interpretation of JavaScript is from top to bottom and goes from left to right. SpiderMonkey and Rhino are few of the early JavaScript engines that were implemented by different browsers, such as Netscape Navigator and Mozilla Firefox.

Next is our simple Hello World program; the JavaScript program is in between the <script> tags in the head section. The script tags can either be added to the head tag or to the body tag. As JavaScript is not non-blocking, the scripts hold the page until they are loaded. It is common to see the scripts being loaded at the end; this would work if there were no dependencies to other files or elements. One such example of a dependency would be a library that is used from a different location. We will be looking at a lot of these examples in the later chapters. We will be discussing the role of Unobtrusive JavaScript at a later point. For our Hello World program, use a text editor of your choice, and save this program with an HTML extension. Load the file in a web browser, and a pop-up box with the text Hello World! should be loaded on the page.

The following code snippet is the first_script.html file:

The output is as follows:

主站蜘蛛池模板: 宜兰市| 尼木县| 宿州市| 瑞金市| 托克逊县| 浦城县| 黔东| 彭州市| 油尖旺区| 正定县| 南宁市| 忻城县| 土默特右旗| 荣成市| 岑溪市| 巴彦淖尔市| 化州市| 个旧市| 古丈县| 灵武市| 政和县| 康马县| 凌云县| 大化| 云阳县| 视频| 资中县| 黔江区| 两当县| 禄劝| 孝昌县| 蓬莱市| 无极县| 巧家县| 龙陵县| 腾冲县| 栾川县| 邯郸县| 沿河| 鄂托克旗| 塔河县|