|
We'll look at using javascript to modify the DOM in response to events. Also, we'll consider an extended example of refactoring some javascript (in the process, illustrating some standard ways of modifying the DOM).
You have already talked about javascript code and the DOM in WebI; we'll review those concepts in the context of improving an actual web page.
video (25m12s)We think of HTML as being a tree. (Or, a way of representing a document's tree-structure as a flat string full of angle-brackets.) The HTML gives content-information associated with nodes of the tree; CSS further provides presentation-information associated with nodes. And the browser even tracks more information with each node: all its default-styles and spacing, and more we'll see below.
Barland's definition: DOM (“Document Object Model”): The tree object, as it exists in-memory, representing the web-page; it can be modified after the page is loaded.
Do not attempt to write any significant javascript without a decent debugging tool like Firebug or Chrome Tools' javascript console window help. At least, not if you value your sanity. In particular:
pro-tip (Chrome): If you (a) click on the square-with-pointer icon in upper-left of developer-tools, and then (b) click on something on the HTML page, it’ll warp straight to the DOM-node corresponding to your upper-window-selection!
Javascript in a nutshell:
Note that all HTML attributes become DOM corresponding attributes:
if you have “
var someNode = document.getElementById("sample"); var isBarlandSpeakingTruth = (someNode.lang==="en" && someNode.value==="magic-blue"); someNode.style.color = (isBarlandSpeakingTruth ? "blue" : "orange"); |
Weirdnesses/inconsistencies:
Notice what the DOM's text-nodes look like, when there was an entity in the original HTML:
If our html-source has
[Alternately: you can also visually-inspect the contents of that node: In Chrome click on the square-with-pointer icon in upper-left of developer-tools, and then click on: <Here>.
At first, you might think
“the body of that
Chrome reads the "flat" string of HTML with brackets-to-indicate-elements, and constructs the DOM tree from that text. After the tree's been built, there's no reason to be confused if you happened to see text (as the leaf of the DOM) which contains ‘<’ we know that character wouldn't be talking about an open-tag because the tree is already built. So, Chrome goes through the DOM, and if it sees any "<" in a string, it literally replaces them with "<", and it's no longer confusable as an open-tag. (Similarly with other entities.)
So that's why string-length returned the value it did (six not twelve), and why the inspector shows a literal ‘<’ character.
Btw, this difference between source-file and internalized-representation
is reminiscent of how, in Java, the string literal
We'll show-source, and discuss evolution of the javascript program. For each version: discuss how much work it would be to add another category (animal)? (And: what is the least conceivable amount of work/info required?)
As seen in the video, the javascript methods-of-interest are
alternate video: If you don't like the above videos, here is a long (less wieldy) version of the same info, from 2017-Mar-14 distance section (57m05s): introduction to the DOM and javascript to modify it.
In-class example: Applying the above: How can/should we write javascript for a Bingo card? (initial and final versions).
Note: here's
an on-line javascript interpreter.
We'll use it to note that:
arrays are objects;
any object can have properties accessed via square-brackets;
but you should still iterate over arrays by indexing over [0,data.count).
You can also use “
Arrays in javascript are not associative -- the indices are only numeric.
However: any object (array or string or DOM-node or whatever) can have
properties;
properties are key/value pairs.
So I guess every object could be used as an asssociative array incidentally.
This is discouraged, because other code might add properties to your object.
Just to muddle things thoroughly, properties can be assigned/retrieve
either through dot-notation (“
Arrays have a property named “length”. To demo, paste the below into this on-line javascript interpreter.
var a; a = [71,72,73]; a.foo = "huh"; writeln( "The length of a: " + a.length ); writeln( "The length of a: " + a['length'] ); writeln( "Using a regular for-loop, to loop over numeric indices:" ); for (var i = 0; i < a.length; ++i ) { writeln("at index i=" + i + ", a[i]=" + a[i] ); } writeln( "Using for-in, to loop over all *enumerable* properties:" ); for (var i in a) { writeln("at index i=" + i + ", a[i]=" + a[i] ); } writeln( "Note: the for-in loop will also include properties from the object's " + "parent object, and it's parent, etc." ); writeln( "Note: `length` is a property of the object, but the `for` loop doesn't " + "enumerate over it. That's because some properties can be tagged as " + "'non-enumerable' -- a concept invented just for for-each loops?" ); writeln("Using getOwnPropertyNames to see all properties *incl. 'non-enumerable'*:"); writeln( Object.getOwnPropertyNames(a) ); writeln("see more at https://stackoverflow.com/questions/8024149/is-it-possible-to-get-the-non-enumerable-inherited-property-names-of-an-object"); |
Tips for minimizing the difference between php and javascript code:
getDocElement('fname') |
This has the advantage that you can provide a default argument,
to use when the element doesn't exist [especially helpful in
php handling checkboxes, where if no checkboxes were selected
the lookup
This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |