我想做的是在is函数之外调用一个变量,并警告它,直到它返回我的所有内容都是“未定义”。
<!DOCTYPE html>
<html>
<head>
<title>Display IP Address</title>
<style>
body {
background-color: #FFCC00;
}
h1 {
font-family: 'Carrois Gothic', sans-serif;
text-align: center;
padding-top: 140px;
font-size: 60px;
margin: -15px;
}
p {
font-family: 'Carrois Gothic', sans-serif;
color: #907400;
text-align:center;
}
</style>
</head>
<body>
<h1 id=ipText></h1>
<p>( This is your IP address...probably :P )
<script>
var xhr;
var response
xhr = new XMLHttpRequest();
xhr.open('GET', "//ipinfo.io/json", true);
xhr.send();
xhr.addEventListener("readystatechange", processRequest, false);
function processRequest(e) {
if (xhr.readyState == 4 && xhr.status == 200) {
response = JSON.parse(xhr.responseText);
document.querySelector("#ipText").innerHTML = response.ip;
}
}
alert(response)
</script>
</body>
</html>