Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • JavaScript and JSON Essentials
  • Toc
  • feedback
JavaScript and JSON Essentials

JavaScript and JSON Essentials

By : Sai S Sriparasa
3.1 (15)
close
JavaScript and JSON Essentials

JavaScript and JSON Essentials

3.1 (15)
By: Sai S Sriparasa

Overview of this book

The exchange of data over the Internet has been carried out since its inception. Delimiter-separated lists such as CSV and tag-separated languages such as XML are very popular, yet they are considered to be verbose by a section of developers. JSON (JavaScript Object Notation) is a lightweight text-based code to create objects to transfer data over the Internet. It is a data exchange format that is human-readable (like XML, but without the markup around your actual payload) and its syntax is a subset of the JavaScript language that was standardized in 1999. JavaScript and JSON Essentials is a step-by-step guide that will introduce you to JSON and help you understand how the lightweight JSON data format can be used in different ways either to store data locally or to transfer data over the Internet. This book will teach you how to use JSON effectively with JavaScript. This book begins with a brief refresher course on JavaScript before taking you through how JSON data can be transferred via synchronous, asynchronous, and cross-domain asynchronous HTTP calls. JSON is not just about data transfer; this book throws light on the alternate implementations of JSON as well. You will learn the data types that JavaScript uses and how those data types can be used in JSON. You will go through the concepts of how to create, update, parse, and delete a JSON object. You will also look at the different techniques of loading a JSON file onto a web page, how to use jQuery to traverse through an object, and how to perform access operations. You will also go over a few resources that will make debugging JSON quick and easy.
Table of Contents (15 chapters)
close

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:

bookmark search playlist font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete