Skip to content

Instantly share code, notes, and snippets.

@ivanteoh
Created July 6, 2020 11:37
Show Gist options
  • Save ivanteoh/5dc48b1d35beeb3724590f0ea4e866a5 to your computer and use it in GitHub Desktop.
Save ivanteoh/5dc48b1d35beeb3724590f0ea4e866a5 to your computer and use it in GitHub Desktop.
Javascript: 101 Week 4
// from Douglas Crockford's video 'An Incovenient API - The Theory of the DOM'
// http://video.yahoo.com/watch/111582/992708
function walkTheDOM(node, func) {
func(node);
node = node.firstChild;
while (node) {
walkTheDOM(node, func);
node = node.nextSibling;
}
}
function getTextNode() {
var result = [];
// http://forums.htmlhelp.com/index.php?showtopic=5173
var someElementRef = document.getElementsByTagName('body')[0];
// remove all the spaces and new lines
someElementRef.innerHTML = someElementRef.innerHTML.replace(
/\B\s\B|[\n\r\t]/g, '');
walkTheDOM(someElementRef, function(node) {
var nodeName = node.nodeName;
if (nodeName === '#text') {
result.push(node);
}
});
return result;
}
alert(getTextNode().length);
// from Douglas Crockford's video 'An Incovenient API - The Theory of the DOM'
// http://video.yahoo.com/watch/111582/992708
function walkTheDOM(node, func) {
func(node);
node = node.firstChild;
while (node) {
walkTheDOM(node, func);
node = node.nextSibling;
}
}
function getTextNode() {
var result = [];
// http://forums.htmlhelp.com/index.php?showtopic=5173
var someElementRef = document.getElementsByTagName('body')[0];
// remove all the spaces and new lines
someElementRef.innerHTML = someElementRef.innerHTML.replace(
/\B\s\B|[\n\r\t]/g, '');
walkTheDOM(someElementRef, function(node) {
var nodeName = node.nodeName;
if (nodeName === '#text') {
result.push(node);
}
});
return result;
}
var total = getTextNode().length;
var child = document.createTextNode(total);
var parent = document.createElement('span');
parent.appendChild(child);
document.getElementsByTagName('body')[0].lastChild.appendChild(parent);
// from Douglas Crockford's video 'An Incovenient API - The Theory of the DOM'
// http://video.yahoo.com/watch/111582/992708
function walkTheDOM(node, func) {
func(node);
node = node.firstChild;
while (node) {
walkTheDOM(node, func);
node = node.nextSibling;
}
}
function getTextNode() {
var result = [];
// http://forums.htmlhelp.com/index.php?showtopic=5173
var someElementRef = document.getElementsByTagName('body')[0];
// remove all the spaces and new lines
someElementRef.innerHTML = someElementRef.innerHTML.replace(
/\B\s\B|[\n\r\t]/g, '');
walkTheDOM(someElementRef, function(node) {
var nodeName = node.nodeName;
if (nodeName === '#text') {
result.push(node);
}
});
return result;
}
// total is not include the newly elements
var total = getTextNode().length;
var child = document.createTextNode(total);
var parent = document.createElement('span');
parent.setAttribute('id','special_span');
parent.appendChild(child);
document.getElementsByTagName('body')[0].lastChild.appendChild(parent);
var bold = document.createTextNode('bold');
var link = document.createElement('a');
link.setAttribute('href','#');
link.setAttribute('onclick',
'document.getElementById("special_span").innerHTML="<strong>"' +
'+document.getElementById("special_span").innerHTML+"</strong>"');
link.appendChild(bold);
document.getElementsByTagName('body')[0].appendChild(link);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment