The recommended way: external file in the header
myfile.html
<!DOCTYPE html>
<html lang="en">
<head>
…
<script type="text/javascript"
src="myscript.hs"></script>
…
</head>
…
</html>
myscript.js
alert("Hello World!");
…
…
The recommended way: or at the bottom of the page
myfile.html
<!DOCTYPE html>
<html lang="en">
<head>
…
</head>
<body>
…
<script type="text/javascript"
src="myscript.hs"></script>
</body>
</html>myscript.js
alert("Hello World!");
…
…
window object
In particular
window.locationwindow.navigatorwindow.historywindow.history.back()window.history.forward()window.documentn of the tree
n.nodeTypen.nodeNamen.nodeValuen.parentNoden.childNodesn.innerHTMLn.stylen.getAttribute("attr")n.setAttribute("attr","val")n of the tree
With d = window.document
d.getElementsByTagName("h1");d.getElementById("someID");undefinedd.getElementsByClassName("classN");d.createElement(tagName);d.createElementNS(namespaceURI, tagName);d.createTextElement("some text");parent.insertBefore(nodeToInsert, childRef);parent.appendChild(nodeToInsert);parent.removeChild(nodeToRemove);nodeToRemove.remove();Attaching actions to events
(Otherwise nothing happens once the page is loaded)
You can find a complete list e.g. here
Mouse click
<span onclick="console.log('Hello !');">Click me!</span>
Form change
<form>
<input type="text" value="default text"
onchange="alert('Booh!');" >
</form>
When the page is loaded
<body onload="console.log('Webpage loaded');" >
..
</body>
Setting up a default handler
someDOMelement.onclick = function(e){
// what to do on a click
};
Or adding a new handler to the list of handlers
someDOMelement.addEventListener("click",function(e){
// what to do on a click
},false);