My HTML file
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<p>Hello World</p>
<script type="text/javascript" src="hello.js"></script>
</body>
</html>
My javascript file
var button = document.createElement("button");
button.id = "test";
button.innerHTML = "Test";
button.addEventListener("click", function() {
alert("hello");
});
var body = document.body;
body.appendChild(button);
I don’t understand why when I click Test button, console of Chrome displays error “Uncaught Error: INDEX_SIZE_ERR: DOM Exception 1” . But I realize that error appears only if I run it on localhost (I use apache of xampp as localhost) . If I run as normal file:// , it successes without any error
It works fine for me….
But any ways please use window.onload function to do any stuff from javascript.
So do something like this