Tuesday, 9 September 2014

window.addEventListener on page load

<!DOCTYPE html>
<html>
<body>
<p>Click anywhere in the document.</p>
<p id="demo"></p>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
function callOnload() {
document.getElementById("demo2").innerHTML = "I am from Onload";
}
//On page load
if (window.addEventListener) {
window.addEventListener('load', callOnload, false); //W3C
}
else {
window.attachEvent('onload', callOnload); //IE
}
//On click of page
document.addEventListener("click", function () {
document.getElementById("demo").innerHTML = "Hello World!";
});
//On click of page
window.addEventListener("click", myFunction, false);
function myFunction() {
document.getElementById("demo1").innerHTML = "Welcome ...";
}
</script>
</body>
</html>
view raw gistfile1.html hosted with ❤ by GitHub