docs

Introduction to Web APIs

Web APIs are a powerful feature of modern web development that allow web pages and applications to interact with external resources, services, or systems programmatically. They enable developers to tap into browser functionality, access device sensors, make HTTP requests to servers, and manipulate elements on a webpage.

Let’s explore these concepts in more detail.


What are APIs?

APIs (Application Programming Interfaces) are sets of rules and protocols that allow different software applications to communicate with each other. In the context of web development, APIs enable interactions between web browsers and web servers or between different systems.

Key Features:

In a web environment, APIs can be categorized into:

  1. Browser APIs: Built into the web browser (e.g., DOM API, Fetch API, Canvas API).
  2. Third-party APIs: Provided by external services, such as Twitter API, Google Maps API, or weather services.

What Can APIs Do?

APIs have a broad range of functionalities and can do everything from allowing websites to interact with user data, to enabling the integration of complex third-party services. Some examples of what web APIs can do include:

Examples of Browser APIs:


How Do APIs Work?

Web APIs typically work using a request-response mechanism. This involves one system sending a request to an API, and the API returning a response. The exchange is most often done via HTTP, the protocol used by the web.

Here’s a general overview of how web APIs work:

  1. Client Request: The client (e.g., a web browser) makes a request to an API using a method such as GET, POST, PUT, or DELETE. This request is often made using the Fetch API or XMLHttpRequest in JavaScript.
  2. API Processes the Request: The server hosting the API processes the request. It might retrieve data from a database, run a computation, or interact with another service.
  3. Response Sent Back: Once the request is processed, the API sends a response back to the client. This response usually contains data (often in JSON or XML format) or a status code indicating the success or failure of the request.

  4. Client Processes Response: The client processes the response. For example, if a web page requested data, JavaScript would use the response data to update the user interface dynamically.

Request-Response Example:

Using the Fetch API to make a request to an external API (e.g., a weather service):

fetch("https://api.weatherapi.com/v1/current.json?key=YOUR_API_KEY&q=London")
  .then((response) => response.json())
  .then((data) => {
    console.log("Weather Data:", data);
  })
  .catch((error) => console.error("Error:", error));

In this example:


Common Types of Web APIs


Web API Example

Let’s take an example of interacting with a Geolocation API to get a user’s current location:

if ("geolocation" in navigator) {
  navigator.geolocation.getCurrentPosition(function (position) {
    console.log("Latitude: " + position.coords.latitude);
    console.log("Longitude: " + position.coords.longitude);
  });
} else {
  console.log("Geolocation is not supported by this browser.");
}

In this case, the Geolocation API retrieves the user’s current location, which can be used to display relevant content based on their physical location.


Conclusion

Web APIs are an essential part of modern web development, enabling web applications to interact with external services, browser features, and devices. Whether you’re retrieving data from a server or accessing device sensors, APIs provide the foundation for building dynamic, interactive web applications.

To learn more about Web APIs, you can refer to MDN Web Docs: Introduction to Web APIs.

Manipulating Documents

The ability to manipulate HTML documents dynamically is one of the most powerful aspects of modern JavaScript. It allows web developers to create interactive, responsive, and engaging websites. This process of manipulating documents in JavaScript involves modifying, adding, or removing HTML elements, attributes, and styles on a web page after it has loaded, using the Document Object Model (DOM).

The DOM is a programming interface for web documents that represents the structure of a webpage as a tree of objects. Through JavaScript, you can interact with and modify this structure dynamically.


The Important Parts of a Web Browser

To understand how document manipulation works, it’s helpful to know the key parts of a web browser that are involved in this process:

  1. Rendering Engine: This part of the browser is responsible for converting HTML and CSS into a visual representation on the screen. The rendering engine interprets the structure of the HTML document and applies styles, creating the visible page.
  2. JavaScript Engine: Modern browsers include JavaScript engines (such as V8 in Chrome and SpiderMonkey in Firefox) that execute JavaScript code. This code interacts with the DOM to manipulate the structure and content of the webpage in real time.
  3. Document Object Model (DOM): The DOM represents the structure of an HTML or XML document. It is a tree-like representation where every element and attribute is an object, and JavaScript can interact with these objects to make changes.

The Document Object Model (DOM)

The DOM represents the HTML structure as a hierarchy of nodes (HTML elements). This allows JavaScript to interact with the webpage. The browser creates the DOM when the page is loaded, and any changes you make to the DOM will reflect immediately on the page.

Key DOM Concepts:

For example, in the following HTML snippet:

<ul>
  <li>Item 1</li>
  <li>Item 2</li>
</ul>

The <ul> element is the parent node, and the <li> elements are its child nodes.

Basic DOM Manipulation:

Example:

document.getElementById("myElement").textContent = "New Content";
document.querySelector("p").style.color = "blue";

Active Learning: Basic DOM Manipulation

Let’s dive into an interactive example of basic DOM manipulation. Suppose you want to change the text of a paragraph element when a button is clicked. Here’s a simple HTML structure and JavaScript code:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>DOM Manipulation</title>
  </head>
  <body>
    <p id="myParagraph">This is the original text.</p>
    <button onclick="changeText()">Change Text</button>

    <script>
      function changeText() {
        document.getElementById("myParagraph").textContent =
          "Text has been changed!";
      }
    </script>
  </body>
</html>

In this example:


Active Learning: A Dynamic Shopping List

Now, let’s extend the concept of DOM manipulation by creating a dynamic shopping list. Imagine you want to add items to a list interactively using JavaScript.

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Dynamic Shopping List</title>
  </head>
  <body>
    <h1>Shopping List</h1>
    <ul id="shoppingList"></ul>

    <input type="text" id="newItem" placeholder="Enter a new item" />
    <button onclick="addItem()">Add Item</button>

    <script>
      function addItem() {
        const itemText = document.getElementById("newItem").value;
        if (itemText === "") return; // If the input is empty, do nothing

        const newItem = document.createElement("li"); // Create a new list item
        newItem.textContent = itemText; // Set the text content of the new item
        document.getElementById("shoppingList").appendChild(newItem); // Append the new item to the list

        document.getElementById("newItem").value = ""; // Clear the input field
      }
    </script>
  </body>
</html>

In this example:


Additional DOM Manipulation Concepts

Adding and Removing Elements

You can dynamically add or remove elements from the DOM using JavaScript. For instance:

Modifying Attributes and Styles

You can modify element attributes and styles using methods like .setAttribute(), .getAttribute(), and .removeAttribute(). For example:

document.querySelector("img").setAttribute("src", "newImage.jpg");

Similarly, styles can be changed:

document.querySelector("p").style.color = "red";

Event Handling

DOM manipulation is often tied to user events, such as clicks, keypresses, or form submissions. You can attach event listeners to elements using addEventListener(), which triggers specific actions when the event occurs.

document.querySelector("button").addEventListener("click", () => {
  alert("Button clicked!");
});

Conclusion

Manipulating documents using JavaScript and the DOM API is a crucial aspect of client-side development. By understanding how to interact with the DOM, developers can create dynamic, interactive web applications that respond to user input and change without requiring page reloads.

To learn more about manipulating documents in JavaScript, you can refer to MDN Web Docs: Manipulating Documents.

Fetching Data from the Server

Fetching data from the server is a critical aspect of web development, especially when building dynamic and interactive websites. It allows your web applications to communicate with a server, retrieve data, and dynamically update the content displayed to users. JavaScript provides several ways to fetch data from a server, including the modern Fetch API and the older XMLHttpRequest API.

When you make a request to a server, you’re usually asking for some resource, such as a webpage, image, or data in JSON format. Fetching data is typically done asynchronously so that your application can continue running without waiting for the server’s response.


What is the Problem Here?

Before the introduction of modern APIs, fetching data from servers was cumbersome. The main problem was that web pages were static—every time a user performed an action, like clicking a button, the entire page had to reload to fetch new data from the server. This created a poor user experience, slowing down websites and requiring full-page refreshes.

To solve this, asynchronous JavaScript techniques like AJAX were introduced to allow fetching data from the server without reloading the entire page. However, the APIs (like XMLHttpRequest) used for this process were often complex and verbose.


The Fetch API

The Fetch API is a modern, promise-based API for making network requests. It provides a more flexible and simpler way to fetch resources from the network, compared to its predecessor, XMLHttpRequest. The Fetch API is native to modern browsers and is built on Promises, which means it handles asynchronous operations more easily and allows for chaining.

Basic Syntax

fetch("https://api.example.com/data")
  .then((response) => {
    if (!response.ok) {
      throw new Error("Network response was not ok");
    }
    return response.json();
  })
  .then((data) => {
    console.log(data);
  })
  .catch((error) => {
    console.error("There was a problem with the fetch operation:", error);
  });

In this example:

Features of the Fetch API:

  1. Simplified Syntax: The Fetch API provides a cleaner, more intuitive syntax compared to XMLHttpRequest.
  2. Response Handling: The response is represented as a Response object, which contains information about the request and methods to parse the body (e.g., .json(), .text()).
  3. Promise-based: Since Fetch is promise-based, you can chain .then() for success and .catch() for error handling.
  4. CORS (Cross-Origin Resource Sharing): The Fetch API handles CORS-related security policies more robustly than XMLHttpRequest.

Example with Fetch API:

fetch("https://api.github.com/users/octocat")
  .then((response) => response.json())
  .then((data) => console.log(data))
  .catch((error) => console.error("Error fetching data:", error));

Handling Errors in Fetch

One of the notable things about Fetch is that it doesn’t automatically reject the Promise if the response is an HTTP error (like 404 or 500). Instead, it considers only network failures as errors. You have to explicitly check the response.ok property to see if the request succeeded.

fetch("https://api.example.com/data")
  .then((response) => {
    if (!response.ok) {
      throw new Error("Network response was not ok");
    }
    return response.json();
  })
  .catch((error) => {
    console.error("Fetch error:", error);
  });

The XMLHttpRequest API

XMLHttpRequest (XHR) is an older way of making asynchronous requests to a server, introduced in the late 1990s. It was commonly used in AJAX applications to fetch data from servers without reloading the page. Though still supported in modern browsers, it has been mostly replaced by the Fetch API due to its simplicity.

Basic Syntax with XMLHttpRequest:

var xhr = new XMLHttpRequest();
xhr.open("GET", "https://api.example.com/data", true);
xhr.onload = function () {
  if (xhr.status === 200) {
    console.log(JSON.parse(xhr.responseText));
  } else {
    console.error("Error fetching data:", xhr.status);
  }
};
xhr.onerror = function () {
  console.error("Request failed");
};
xhr.send();

In this example:

Disadvantages of XMLHttpRequest:


Comparison: Fetch API vs XMLHttpRequest

Feature Fetch API XMLHttpRequest (XHR)
Simplicity Much simpler, concise syntax More verbose, requires multiple steps
Promise-based Yes, provides native Promise support No, relies on callbacks
Error handling Requires manual checking of response.ok Provides onload and onerror events
CORS Handling More modern handling of CORS CORS requires more manual setup with XHR
Support for Streams Supports response streaming, enabling processing as it downloads Not natively supported

Conclusion

Both the Fetch API and the XMLHttpRequest API serve the same purpose of fetching resources from a server, but the Fetch API offers a more modern, efficient, and simpler way to handle asynchronous requests. While XMLHttpRequest is still supported, Fetch should be used in modern JavaScript applications because it is more versatile, offers better error handling, and integrates well with promises.

To learn more about fetching data in JavaScript, you can refer to MDN Web Docs: Fetching Data.

Drawing Graphics in JavaScript

Drawing graphics in JavaScript is primarily done using the <canvas> element or WebGL for more advanced 3D graphics. This allows you to create dynamic, interactive, and animated visual content directly on the web without requiring plugins like Flash. JavaScript provides an easy way to draw shapes, images, and animations on the web by working with the canvas API or WebGL.

You can refer to the full documentation on MDN Web Docs: Drawing Graphics for more context.


Graphics on the Web

Graphics on the web are an integral part of modern websites. They enhance user interaction, make content more engaging, and can provide complex visual data representations like charts, diagrams, and animations.

The web offers two primary ways of working with graphics in JavaScript:

  1. Canvas API: This is a simple 2D drawing API that allows you to draw shapes, text, images, and animations in a <canvas> element.
  2. WebGL: A more advanced API that allows you to create 3D graphics and utilize hardware acceleration via the GPU (Graphics Processing Unit).

Active Learning: Getting Started with a <canvas>

The <canvas> element is at the heart of the Canvas API. It provides a region in your HTML page where you can draw 2D graphics.

To start working with a canvas:

  1. Add a <canvas> element to your HTML:

    <canvas id="myCanvas" width="500" height="500"></canvas>
    

    The width and height attributes define the size of the canvas. These attributes can also be set via CSS.

  2. Access the canvas via JavaScript and begin drawing:

    const canvas = document.getElementById("myCanvas");
    const ctx = canvas.getContext("2d"); // Get the drawing context (2D)
    
    // Draw a rectangle
    ctx.fillStyle = "green";
    ctx.fillRect(10, 10, 150, 100);
    

Here, getContext('2d') tells the browser that you want to use the 2D drawing functions, and you can then use the ctx object to draw shapes like rectangles, circles, or paths on the canvas.

Example: Drawing a circle

ctx.beginPath();
ctx.arc(150, 75, 50, 0, 2 * Math.PI);
ctx.stroke();

This code draws a circle on the canvas by specifying the center coordinates (150, 75), radius 50, and the full circle (0 to 2 * Math.PI).


2D Canvas Basics

Once you have your canvas element set up, you can use various drawing methods to create complex images and animations. Some key methods include:


Loops and Animations

Canvas animations can be created using JavaScript’s setInterval(), setTimeout(), or more commonly, requestAnimationFrame() to repeatedly redraw elements on the canvas.

Basic Animation Example:

let x = 0;

function draw() {
  ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear the canvas
  ctx.fillRect(x, 50, 50, 50); // Draw a square
  x += 2; // Move the square
  requestAnimationFrame(draw); // Keep animating
}

draw(); // Start the animation

Here, the draw() function is repeatedly called using requestAnimationFrame(), creating smooth animations.


WebGL

WebGL (Web Graphics Library) is an API for rendering 3D graphics within any compatible browser, using the HTML5 <canvas> element. Unlike the 2D canvas API, WebGL provides a more powerful and complex interface, enabling interaction with the graphics hardware directly.

To work with WebGL:

  1. You start by retrieving the WebGL context from the canvas.
    const canvas = document.getElementById("myCanvas");
    const gl = canvas.getContext("webgl");
    
  2. You can create shaders, render 3D models, and apply lighting effects, all by writing WebGL code, which includes both JavaScript and GLSL (OpenGL Shading Language).

WebGL is used in gaming, 3D modeling, and other applications requiring high-performance 3D rendering.


Summary

For more information and tutorials, you can refer to MDN’s Drawing Graphics Documentation.

Video and Audio APIs in JavaScript

The Video and Audio APIs allow developers to embed and control multimedia content like video and audio on web pages. With JavaScript and the HTML5 <video> and <audio> elements, it’s easy to manipulate media playback, control volume, add custom controls, and respond to user interactions.

For more in-depth knowledge, you can refer to MDN’s Video and Audio APIs Documentation.


HTML Video and Audio

HTML5 introduced the <video> and <audio> elements, which allow native embedding of media files in web pages without the need for external plugins like Flash. These elements come with built-in controls, such as play, pause, and volume, and they provide an easy way to play media directly in the browser.

Basic Structure of <video> and <audio> Elements:

Both elements support multiple formats, but you must include fallback text or multiple sources to ensure compatibility across different browsers.


The HTMLMediaElement API

The HTMLMediaElement API provides JavaScript methods and properties for interacting with media elements (<video> and <audio>). This API allows for greater control over media playback, beyond the native browser controls.

Common Methods in HTMLMediaElement API:

Common Properties in HTMLMediaElement API:

Event Listeners:

The HTMLMediaElement API also supports a variety of events that you can listen to for more control over media:


Summary

In summary, the Video and Audio APIs allow developers to incorporate rich multimedia content into their web applications. With the introduction of HTML5’s <video> and <audio> elements, and the capabilities provided by the HTMLMediaElement API, developers can create dynamic, interactive media experiences.

Key takeaways include:

  1. HTML5 Media Elements: The <video> and <audio> tags allow native embedding and playback of media files.
  2. HTMLMediaElement API: Provides fine-grained control over media, with methods and properties to play, pause, manipulate volume, handle events, and much more.

For more details, refer to the full documentation on MDN’s Video and Audio APIs.

Client-Side Storage in JavaScript

Client-side storage allows web applications to store data directly within the user’s browser. This enables apps to save information like preferences, offline data, or large sets of records, even when the user isn’t connected to the internet.

For a full understanding, refer to the complete guide at MDN: Client-Side Storage.


What is Client-Side Storage?

Client-side storage refers to various methods and APIs that allow data to be saved directly on the user’s device through their browser. This data can persist across sessions or be temporary, depending on the method used.

Key use cases:

There are several mechanisms for client-side storage, including:

  1. Web Storage: Simplified storage of key-value pairs.
  2. IndexedDB: For storing more complex, structured data.
  3. Service Workers and Cache API: For storing assets like HTML, CSS, JS, and media for offline access.

Storing Simple Data — Web Storage

Web storage offers two primary APIs: localStorage and sessionStorage. Both APIs store data as key-value pairs in the browser, but they differ in terms of data persistence.

1. localStorage:

Example:

// Store data
localStorage.setItem("theme", "dark");

// Retrieve data
let theme = localStorage.getItem("theme");
console.log(theme); // Output: 'dark'

// Remove data
localStorage.removeItem("theme");

// Clear all data
localStorage.clear();

2. sessionStorage:

Example:

// Store session data
sessionStorage.setItem("pageView", "home");

// Retrieve data
let pageView = sessionStorage.getItem("pageView");
console.log(pageView); // Output: 'home'

// Clear session storage
sessionStorage.clear();

Storing Complex Data — IndexedDB

IndexedDB is a powerful low-level API that allows for the storage of large amounts of structured data, including files and blobs. It operates asynchronously and supports transactions for reliability.

Key Features:

Example Usage:

  1. Opening a Database:

    let request = indexedDB.open("myDatabase", 1);
    
    request.onupgradeneeded = function (event) {
      let db = event.target.result;
      let objectStore = db.createObjectStore("users", { keyPath: "id" });
      objectStore.createIndex("name", "name", { unique: false });
    };
    
  2. Adding Data:

    let transaction = db.transaction(["users"], "readwrite");
    let objectStore = transaction.objectStore("users");
    let request = objectStore.add({ id: 1, name: "John Doe" });
    
    request.onsuccess = function (event) {
      console.log("User added to the store");
    };
    

IndexedDB is ideal for larger and more complex data structures, like collections of objects or relational data.


Offline Asset Storage

To make web applications functional even when the user is offline, JavaScript provides APIs like Service Workers and the Cache API. These allow web apps to cache assets such as HTML, CSS, and JavaScript files locally, ensuring that they can be accessed offline.

Service Workers:

Cache API:

Example of caching a file with a Service Worker:

self.addEventListener("install", function (event) {
  event.waitUntil(
    caches.open("v1").then(function (cache) {
      return cache.addAll(["/index.html", "/styles.css", "/script.js"]);
    })
  );
});

When the user loads your app, the service worker serves the cached files when the network is unavailable, enabling offline functionality.


Summary

Client-side storage offers multiple ways to store data locally on the user’s browser:

  1. Web Storage: Local and session storage for simple key-value pair data.
  2. IndexedDB: A more advanced, asynchronous database for complex data.
  3. Offline Asset Storage: Service Workers and Cache API for caching resources for offline use.

Each method serves different purposes and can be combined to create powerful offline-ready applications. Depending on your needs (simple vs. complex data), you can choose between these storage mechanisms.

For more detailed information, refer to MDN’s Client-Side Storage Guide.