Skip to main content

Posts

Showing posts with the label JavaScript Tutorial

JavaScript - Window Navigator

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.appName navigator.appCodeName navigator.platform Navigator Cookie Enabled The property cookieEnabled returns true if cookies are enabled, otherwise false: Example < p  id= "demo" > < /p > < script > document.getElementById("demo").innerHTML = "Cookies Enabled is " + navigator.cookieEnabled; < /script > The Browser Names The properties  appName  and  appCodeName  return the name of the browser: Example < p  id= "demo" > < /p > < script > document.getElementById("demo").innerHTML = "Name is " + navigator.appName + ". Code name is " + navigator.appCodeName; < /script > Did you know? IE11, Chrome,

JavaScript - Popup Boxes

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. Syntax window.alert(" sometext "); The  window.alert()  method can be written without the window prefix. Example alert("I am an alert box!"); Confirm Box A confirm box is often used if you want the user to verify or accept something. When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed. If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false. Syntax window.confirm(" sometext "); The  window.confirm()  method can be written without the window prefix. Example var r = confirm("Press a but

JavaScript - Testing Prototype

JavaScript - Testing Prototype Testing JavaScript Framework Libraries - Prototype Including Prototype To test a JavaScript library, you need to include it in your web page. To include a library, use the <script> tag with the src attribute set to the URL of the library: Including Prototype < !DOCTYPE  html > < html > < head > < script  src= "http://ajax.googleapis.com/ajax/libs/prototype/1.7.2.0/prototype.js" > < /script > < /head > < body > . . . Prototype Described Prototype provides functions to make HTML DOM programming easier. Like jQuery, Prototype has its $() function. The $() function accepts HTML DOM element id values (or DOM elements), and adds new functionality to DOM objects. Unlike jQuery, Prototype has no ready() method to take the place of window.onload(). Instead, Prototype adds extensions to the browser and the HTML DOM. In JavaScript, you can assign a function

JavaScript - Scope and Events

JavaScript Scope Scope is the set of variables you have access to. In JavaScript, objects and functions are also variables. In JavaScript, scope is the set of variables, objects, and functions you have access to. JavaScript has function scope: The scope changes inside functions. Local JavaScript Variables Variables declared within a JavaScript function, become  LOCAL  to the function. Local variables have  local scope : They can only be accessed within the function. Example // code here can not use carName function myFunction() {     var carName = "Volvo";      // code here can use carName } Since local variables are only recognized inside their functions, variables with the same name can be used in different functions. Local variables are created when a function starts, and deleted when the function is completed. Global JavaScript Variables A variable declared outside a function, becomes  GLOBAL . A

JavaScript Array Methods

JavaScript Arrays JavaScript arrays are used to store multiple values in a single variable. Displaying Arrays In this tutorial we will use a script to display arrays inside a <p> element with id="demo": Example < p  id= "demo" > < /p > < script > var cars = ["Saab", "Volvo", "BMW"]; document.getElementById("demo").innerHTML = cars; < /script > The first line (in the script) creates an array named cars. The second line "finds" the element with id="demo", and "displays" the array in the "innerHTML" of it. Example var cars = ["Saab", "Volvo", "BMW"]; Spaces and line breaks are not important. A declaration can span multiple lines: Example var cars = [     "Saab",     "Volvo",     "BMW" ]; Never put a comma after the last element (like &

JavaScript conditions

JavaScript If...Else Statements Conditional statements are used to perform different actions based on different conditions. Conditional Statements Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. In JavaScript we have the following conditional statements: Use  if  to specify a block of code to be executed, if a specified condition is true Use  else  to specify a block of code to be executed if the same condition is false Use  else if  to specify a new condition to test, if the first condition is false Use  the switch  to specify many alternative blocks of code to be executed The if Statement Use the  if  statement to specify a block of JavaScript code to be executed if a condition is true. Syntax if ( condition ) {     block of code to be executed if the condition is true } Note that  it  is in lowercase letters. Uppercase

JavaScript Overview

JavaScript Overview JavaScript is a lightweight, interpreted programming language with object-oriented capabilities that allows you to build interactivity into otherwise static HTML pages. The general-purpose core of the language has been embedded in Internet Explorer, Firefox, and other web browsers The ECMA-262 Specification defined a standard version of the core JavaScript language. JavaScript is: JavaScript is a lightweight, interpreted programming language Designed for creating network-centric applications Complementary to and integrated with Java Complementary to and integrated with HTML Open and cross-platform Client-side JavaScript: Client-side JavaScript is the most common form of the language. The script should be included in or referenced by an HTML document for the code to be interpreted by the browser. It means that a web page need no longer be static HTML, but can include programs that interact with the user, control the browser, and d