Building Interactive JavaScript Websites Flashcards
HTML script element src attribute
The src attribute of a element is used to point to the location of a script file.
The file referenced can be local (using a relative path) or hosted elsewhere (using an absolute path).
HTML script element defer attribute
The defer attribute of a tag is a boolean attribute used to indicate that the script can be loaded but not executed until after the HTML document is fully parsed. It will only work for externally linked scripts (with a src attribute), and will have no effect if it is applied to an inline script.
In the example code block, the tag will be loaded and parsed before the script is executed due to the defer attribute.
HTML script tag async attribute
Scripts are loaded synchronously as they appear in an HTML file, before the following HTML is loaded and parsed. The async attribute can be used to load the scripts asynchronously, such that they will load in the background without blocking the HTML parser from continuing.
In the example code block, the script will load asynchronously in the background, without blocking the HTML parser.
Nodes in DOM tree and HTML DOM
A node in the DOM tree is the intersection of two branches containing data. Nodes can represent HTML elements, text, attributes, etc. The root node is the top-most node of the tree. The illustration shows a representation of a DOM containing different types of nodes.
The DOM is an interface between scripting languages and a web page’s structure. The browser creates a Document Object Model or DOM for each webpage it renders. The DOM allows scripting languages to access and modify a web page. With the help of DOM, JavaScript has the ability to create dynamic HTML.
The DOM Parent-Child Relationship
The parent-child relationship observed in the DOM is reflected in the HTML nesting syntax.
Elements that are nested inside the opening and closing tag of another element are the children of that element in the DOM.
In the code block, the two
tags are children of the
, and the is the parent of both
tags.
The removeChild() Method
The .removeChild() method removes a specified child from a parent element. We can use this method by calling .removeChild() on the parent node whose child we want to remove, and passing in the child node as the argument.
In the example code block, we are removing iceCream from our groceryList element.
The element.parentNode Property
The .parentNode property of an element can be used to return a reference to its direct parent node. .parentNode can be used on any node.
In the code block above, we are calling on the parentNode of the #first-child element to get a reference to the #parent div element.
The document.createElement() Method
The element.InnerHTML Property
The document Object
The document.getElementById() Method
The .querySelector() Method
The element.onclick Property
The element.appendChild() Method
The element.style Property
The element.style property can be used to access or set the CSS style rules of an element. To do so, values are assigned to the attributes of element.style.
In the example code, blueElement contains the HTML element with the ID colorful-element. By setting the backgroundColor attribute of the style property to blue, the CSS property background-color becomes blue.
Also note that, if the CSS property contains a hyphen, such as font-family or background-color, Camel Case notation is used in Javascript for the attribute name, so background-color becomes backgroundColor.