Javascript Web Storage API

The Web Storage API is a simple syntax for storing and retrieving data in the browser. It is very easy to use:

Example

localStorage.setItem("name", "John Doe");
localStorage.getItem("name");

The Web Storage API is supported in all browsers:

Chrome IE/Edge Firefox Safari Opera
Yes Yes Yes Yes Yes

Continue reading Javascript Web Storage API

Javascript Web History API

The Web History API provides easy methods to access the windows.history object.

The window.history object contains the URLs (Web Sites) visited by the user.

The Web History API is supported in all browsers:

Chrome Edge Firefox Safari Opera
Yes Yes Yes Yes Yes

The History back() Method

The back() method loads the previous URL in the windows.history list.

It is the same as clicking the “back arrow” in your browser. Continue reading Javascript Web History API

JavaScript Validation API

Constraint Validation DOM Methods

Property Description
checkValidity() Returns true if an input element contains valid data.
setCustomValidity() Sets the validationMessage property of an input element.

If an input field contains invalid data, display a message:

The checkValidity() Method

<input id="id1" type="number" min="100" max="300" required>
<button onclick="myFunction()">OK</button>

<p id="demo"></p>

<script>
function myFunction() {
  const inpObj = document.getElementById("id1");
  if (!inpObj.checkValidity()) {
    document.getElementById("demo").innerHTML = inpObj.validationMessage;
  }
}
</script>

Continue reading JavaScript Validation API

Javascript Web APIs – Introduction

A Web API is a developer’s dream.

  • It can extend the functionality of the browser
  • It can greatly simplify complex functions
  • It can provide easy syntax to complex code

What is Web API?

API stands for Application Programming Interface.

A Web API is an application programming interface for the Web.

A Browser API can extend the functionality of a web browser.

A Server API can extend the functionality of a web server. Continue reading Javascript Web APIs – Introduction

JavaScript Cookies

Cookies let you store user information in web pages.

What are Cookies?

Cookies are data, stored in small text files, on your computer.

When a web server has sent a web page to a browser, the connection is shut down, and the server forgets everything about the user.

Cookies were invented to solve the problem “how to remember information about the user”:

  • When a user visits a web page, his/her name can be stored in a cookie.
  • Next time the user visits the page, the cookie “remembers” his/her name.

Cookies are saved in name-value pairs like:

username = John Doe

When a browser requests a web page from a server, cookies belonging to the page are added to the request. This way the server gets the necessary data to “remember” information about users. Continue reading JavaScript Cookies

JavaScript Timing Events

Timing Events

The window object allows execution of code at specified time intervals.

These time intervals are called timing events.

The two key methods to use with JavaScript are:

    • setTimeout(function, milliseconds)
      Executes a function, after waiting a specified number of milliseconds.

 

  • setInterval(function, milliseconds)
    Same as setTimeout(), but repeats the execution of the function continuously.

The setTimeout() and setInterval() are both methods of the HTML DOM Window object.

Continue reading JavaScript Timing Events

JavaScript Popup Boxes

JavaScript has three kind of popup boxes: Alert box, Confirm box, and Prompt box.

Alert Box

An alert box is often used if you want to make sure information comes through to the user.

When an alert box pops up, the user will have to click “OK” to proceed. Continue reading JavaScript Popup Boxes

JavaScript Window Navigator

The window.navigator object contains information about the visitor’s browser.

Window Navigator

The window.navigator object can be written without the window prefix.

Some examples:

  • navigator.cookieEnabled
  • navigator.appCodeName
  • navigator.platform

Continue reading JavaScript Window Navigator

JavaScript Window History

The window.history object contains the browsers history.

Window History

The window.history object can be written without the window prefix.

To protect the privacy of the users, there are limitations to how JavaScript can access this object.

Some methods:

  • history.back() – same as clicking back in the browser
  • history.forward() – same as clicking forward in the browser

Continue reading JavaScript Window History

JavaScript Window Location

The window.location object can be used to get the current page address (URL) and to redirect the browser to a new page.

Window Location

The window.location object can be written without the window prefix.

Some examples:

  • window.location.href returns the href (URL) of the current page
  • window.location.hostname returns the domain name of the web host
  • window.location.pathname returns the path and filename of the current page
  • window.location.protocol returns the web protocol used (http: or https:)
  • window.location.assign() loads a new document

Continue reading JavaScript Window Location