Saturday, May 12, 2018

JavaScript Scope

Scope determines the accessibility (visibility) of variables.

JavaScript Function Scope
In JavaScript there are two types of scope:

  • Local scope
  • Global scope
JavaScript has function scope: Each function creates ad new scope.

Strict Mode

Global variables are not created automatically in "Strict Mode".


Global Variables in HTML

With JavaScript, the global object is the complete JavaScript environment.
in HTML, the global scope is the window object. All global variables belong to the window object

JavaScript Event

HTML events are "things" that happen to HTML elements.
When JavaScript is used in HTML pages, JavaScript can "react" on these events.

HTMl Events
An HTML event can be something the browser does, or something a user does.
Here are some examples of HTML events:

  • An HTML web page has finished loading
  • An HTML input field was changed.
  • An HTML button was clicked.

Often, when events happen, you may want to do something.
JavaScript lets you execute code when events are detected.
HTML allows event handler attributes, with JavaScript code, to be added to HTML elements.
With single quotes:
<element event='some JavaScript'>
With double quotes:
<element event="some JavaScript">
In the following example, an onclick attribute (with code), is added to a button element:

<button onclick="document.getElementById('demo').innerHTML = Date()">The time is?</button>

In the example above, the JavaScript code changes the content of the element with id="demo". in the next example, the code changes the content of its own element (using thsi.innerHTML):

<button onclick="this.innerHTML = Date()">The time is?</button>


Common HTML Events

Here is a list of some common HTML events:
EventDescription
onchangeAn HTML element has been changed
onclickThe user clicks an HTML element
onmouseoverThe user moves the mouse over an HTML element
onmouseoutThe user moves the mouse away from an HTML element
onkeydownThe user pushes a keyboard key
onloadThe browser has finished loading the page



No comments:

Post a Comment