我想获取我当前的 URL 地址,将其保存到变量中,然后将其传递给 HTML 元素:
var url = document.URL;
document.getElementById("url").innerHTML = url;
我尝试过使用document.URL
,window.location
但window.location.href
它们都不适合我。它什么也不显示。
我的 HTML 是:
<p id="url"></p>
提前致谢!
这是我的源代码:
<!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>