I would like to get my current URL address, save it to a variable and then pass it to an HTML element:
var url = document.URL;
document.getElementById(url).innerHTML = url;
I have tried using document.URL
, window.location
and window.location.href
but none of them works for me. It displays nothing.
My HTML is:
<p id=url></p>
Thanks in advance!
Here is my source code:
<!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>
<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>
</head>
<body>
<input type=button onclick=first() value=Try Me />
<p onmouseover=submit()>Hover over this text.</p>
<p id=url>error</p>
</body>
</html>