Now that you have expanded all your code, we will see that you start document.getElementById("url")too soon before loading the DOM. Move your script to the end of the body tag (see Revised code at the end of this answer).
Scripts that reference DOM elements cannot be executed until these DOM elements are successfully loaded. There are three ways to ensure this.
- script </body>. <body> , , script, , , script .
script, , , ( ), window.onload. , , , , .
script, DOM. , . DOMContentLoaded .
window.location.href URL . URL- .
: http://jsfiddle.net/jfriend00/yGrxU/
, - . , DOM , , document.getElementById() . , javascript body.
, script, javscript. , , - script.
, , , script </body> :
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
input:hover
{
background: black;
color: white;
border: 0;
}
</style>
</head>
<body>
<input type="button" onclick="first()" value="Try Me" />
<p onmouseover="submit()">Hover over this text.</p>
<p id="url">error</p>
<script>
var url = location.href;
document.getElementById("url").innerHTML = url;
function first()
{
var string = "It works!";
write(string);
}
function write(szoveg)
{
alert(szoveg);
}
function submit()
{
var result;
result = confirm("Would you like to confirm your change?");
if(result==true)
window.location="okay.html";
else if(result==false)
alert("You have clicked the \"cancel\" button.");
}
</script>
</body>
</html>