So, you want to change the font size of text in a paragraph?
<p><font size="+1">Nyuck nyuck nyuck!</font></p><p style="font-size: 120%;">Nyuck nyuck nyuck!</p><p>Nyuck nyuck nyuck!</p>
p { font-size: 120% }
HTML allows us to apply IDs and class names to tags — and using CSS selectors allows us to manipulate them
<div id="container">
<p id="content" class="content">
Piofect!
</div>
</div>
#container p.content {
font-face: bold;
}
We want to avoid this — it makes our html pages bigger, and it is ugly
<a href="hello_world.html" onclick="alert('Hello World!'); return false;">Say Hello</a>
We want this:
<a href="hello_world.html">Say Hello</a>
Let's revisit how CSS accomplishes this:
The script tag lets us include a remote JS file, so we are half way there…
| HTML | CSS | JavaScript |
|---|---|---|
<div>Content</div>
|
div { color: #00F; }
|
document.getElementsByTagName('div');
|
<div id="myDiv">Content</div>
|
#myDiv { color: #00F; }
|
document.getElementById('myDiv');
|
<div class="myDiv">Content</div>
|
div.myDiv { color: #00F; }
|
document.getElementsByClassName('myDiv');*
|
This should add quotations around the text in all the blockquote tags
var blockquotes = document.getElementsByTagName('blockquote');
for(i = 0; i < blockquotes.length; i++) {
var preQuote = document.createTextNode("“");
var postQuote = document.createTextNode("”");
blockquotes[i].insertBefore(preQuote, blockquotes[i].childNodes[0]);
blockquotes[i].appendChild(postQuote);
}
This set the class name of the paragraph with an id of “footer” to “green”
var p = document.getElementById('footer');
p.className = "green";
| HTML | XPath |
|---|---|
<div>Content</div>
|
document.evaluate("//div", document, null, XPathResult.ANY_TYPE, null);
|
<div id="myDiv">Content</div>
|
document.evaluate("//*[@id='myDiv']", document, null, XPathResult.ANY_TYPE, null);
|
<div class="myDiv">Content</div>
|
document.evaluate("//div[contains(@class, 'myDiv')]", document, null, XPathResult.ANY_TYPE, null);
|
Note: All of these function return an Array, including the equivalent of document.getElementById()
var Selectors = {
cache: {},
store: function(selector, element) {
this.cache[selector].push(element);
element.runtimeStyle.behavior = "none";
}
}
div.myDiv {
behavior: expression(Selectors.store("div.myDiv", this));
}
This only works during the loading phase of the page
function bind() {
var element = document.getElementById('someAnchor');
element.onclick = function() {
alert('Bound!');
}
}
// for Internet Explorer (using conditional comments)
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
if (this.readyState == "complete") {
bind(); // call the onload handler
}
};
/*@end @*/
The defer function convieniently defers the execution of the supplied script until AFTER the DOM loads. Excellent!
if (/WebKit/i.test(navigator.userAgent)) { // sniff
var _timer = setInterval(function() {
if (/loaded|complete/.test(document.readyState)) {
clearInterval(_timer);
bind(); // call the onload handler
}
}, 10);
}
This one just checks for the status of the document.readyState flag every 10 ms. Not great but works.
You know what? There are libraries for these sorts of things, out very own Andrew Tetlaw has a fine example: FastInit.
var json = {
id: 8,
title: 'Object number 8',
body: 'This is a serialised version of a JavaScript object',
childIDs: [4, 6, 4, 7]
};
var instance = eval(json);
Allows you to reference nodes just like you would with CSS — via selectors.
var rules = {
'#navigation li' : function(element) {
element.onclick = function() {
alert('You clicked me!');
}
}
};
Behaviour.register(rules);
Try to fix the discrepencies in browser implentations
Make is easy to create animations and effects, such as drop and drag