title | created | modified |
---|---|---|
JS for Web |
2021-05-29T09:07:47.873Z |
2022-03-27T12:25:55.025Z |
-
Execution context At the start of a js thread, there is one context available i.e global object(also referenced with
this
). In browser global object iswindow
and in node, it isglobal
itself. -
Lexical environment Lexical scope(available data + variables where the function was defined) determines our available variables. Not where the function is called(dynamic scope). Everytime we create a function, a new lexical scope created.
-
Hoisting
<script src="example.js" defer></script>
- It specifies that script should be executed after HTML file is completely parsed. So, it delays the execution. It is useful when a script contains functionality that requires interaction with DOM
- It loads and executes the script asynchrously. It means that script will not wait until the entire page is parsed; it will execute immediately after it has been downloaded.
- It is useful for scripts that are independent of other scripts. If it doesn't matter exactly at which point script file is executed, then it is the most suitable option.
The Document Object Model, abbreviated DOM, is a powerful tree-like structure that allows programmers to conceptualize hierarchy and access the elements on a web page.
The DOM is a logical tree-like Model that organizes a web page’s HTML Document as an Object.
Note: There are other types of documents, such as XML and SVG, that are also modeled as DOM structures.
The .querySelector()
method allows us to specify a CSS selector and then returns the first element that matches that selector.
document.querySelector('p');
document.getElementById('bio').innerHTML = 'The description';
let blueElement = document.querySelector('.blue');
blueElement.style.backgroundColor = 'blue';
NOTE : Unlike CSS, the DOM style property does not implement a hyphen such as background-color, but rather camel case notation backgroundColor
let paragraph = document.createElement('p'); //creates an empty element
paragraph.id = 'info'; // assigns an id
paragraph.innerHTML = 'The text inside the paragraph'; //sets innerHTML
document.body.appendChild(paragraph); //inserts it at last
let paragraph = document.querySelector('p');
document.body.removeChild(paragraph);
document.getElementById('sign').hidden = true; // just hides it when loaded initially
let child = document.getElementById('oaxaca');
let parent = document.getElementById('more-destinations');
parent.removeChild(child);
let element = document.querySelector("button");
function turnButtonRed (){
element.style.backgroundColor = 'red';
element.style.color = 'white';
element.innerHTML = 'Red Button';
}
element.onclick = turnButtonRed;
let first = document.body.firstChild; //first child of body element
first.innerHTML = 'I am the child!';
first.parentNode.innerHTML = 'I am the parent and my inner HTML has been replaced!'; //parent node of first i.e, body
let eventTarget = document.getElementById('targetElement');
eventTarget.addEventListener('click', function() {
// this block of code will run when click event happens on eventTarget element
});
Event Handlers can also be registered by setting an .onevent
property on a DOM element (event target).
eventTarget.onclick = eventHandlerFunction;
There are two ways of event propagation in the HTML DOM, bubbling and capturing.
Event propagation is a way of defining the element order when an event occurs. If you have a <p>
element inside a <div>
element, and the user clicks on the <p>
element, which element's "click" event should be handled first?
In bubbling the inner most element's event is handled first and then the outer: the <p>
element's click event is handled first, then the <div>
element's click event.
In capturing the outer most element's event is handled first and then the inner: the
element's click event.
With the addEventListener()
method you can specify the propagation type by using the "useCapture
" parameter:
addEventListener(event, function, useCapture);
The default value is false, which will use the bubbling propagation, when the value is set to true, the event uses the capturing propagation.
For example:
document.getElementById("myP").addEventListener("click", myFunction, true);
document.getElementById("myDiv").addEventListener("click", myFunction, true);
eventTarget.removeEventListener('click', eventHandlerFunction);
Because there can be multiple event handler functions associated with a particular event, .removeEventListener()
needs both the exact event type name and the name of the event handler you want to remove.
NOTE : If .addEventListener()
was provided an anonymous function, then that event listener cannot be removed.
Events are stored as objects. When an event is triggered, the event object can be passed as an argument to the event handler function. Some of the properties of event objects are:
.target
: it references the element that the event is registered to..type
: access the name of event.timestamp
: no of milliseconds that passed since document loaded and event was triggered.
let sharePhoto = function(event) {
event.target.style.display = 'none';
text.innerHTML = event.timeStamp;
}
share.onclick = sharePhoto;