ajax应用
<div class=box id=top_bar>web tools--ajax version </div><br>
<div class="box general"> 天气预报: <input id=weather size=17
value=北京> <input onclick=getweather() type=button value="提 交" name=submit> <span
id=disp_weather></span></div>
<div class="box general"> ip地址查询: <input id=ip maxlength=17 size=17
value=127.0.0.1> <input onclick=getip() type=button value="提 交" name=submit> <span
id=disp_iparea></span></div>
<div class="box general">手机归属查询: <input id=mobile maxlength=15 size=17 value=13>
<input onclick=getmobile() type=button value="提 交"> <span
id=disp_mobilearea></span></div>
<script language=javascript type=text/javascript>
chgbtnstyle();
</script>
//针对不同的浏览器获取指定的id对象
function getobj(objid) {
return document.all ? document.all[objid] : document.getelementbyid(objid);
}
var req;
//使用xmlrequest对象
function createxhr() {
try {
req = new activexobject("msxml2.xmlhttp");
}
catch (e) {
try {
req = new activexobject("microsoft.xmlhttp");
}
catch (e) {
req = false;
}
}
if (!req && typeof xmlhttprequest != "undefined") {
req = new xmlhttprequest();
}
}
function getweather() {
createxhr();
req.open("get", "weather.asp?city=" + getobj("weather").value, true);
req.send();
req.onreadystatechange = handlestatechange1;
}
function handlestatechange1() {
if (req.readystate<4){getobj("disp_weather").innertext = "正在查询["+getobj("weather").value+"]天气状况..."}
else if (req.readystate == 4 && req.status == 200) {
getobj("disp_weather").innertext = req.responsetext;
} else {
getobj("disp_weather").innertext = "\u67e5\u8be2\u8fc7\u7a0b\u51fa\u73b0\u8fc7\u7a0b\uff0c\u8bf7\u91cd\u8bd5\u6216\u8054\u7cfb\u7ba1\u7406\u5458\uff01";
}
}
function checkip() {
var iparray, ip, j;
ip = getobj("ip").value;
if (/[^\d\.]/.test(ip)) {
return false;
}
iparray = ip.split(".");
if (iparray.length != 4) {
return false;
}
for (var i = 0; i < 4; i++) {
if (iparray[i].length == 0 || iparray[i] > 255) {
return false;
}
}
return true;
}
function getip() {
if (checkip()) {
createxhr();
req.open("get", "ipsearch.asp?ip=" + getobj("ip").value, true);
req.send();
req.onreadystatechange = handlestatechange;
} else {
getobj("disp_iparea").innertext = "\u8fd4\u56de\u9519\u8bef\uff1a\u4e0d\u662f\u6b63\u786e\u7684ip";
}
}
function handlestatechange() {
if (req.readystate<4){getobj("disp_iparea").innertext = "正在查询ip["+getobj("ip").value+"]..."}
else if (req.readystate == 4 && req.status == 200) {
getobj("disp_iparea").innertext = req.responsetext;
} else {
getobj("disp_iparea").innertext = "\u67e5\u8be2\u8fc7\u7a0b\u51fa\u73b0\u9519\u8bef\uff0c\u8bf7\u91cd\u8bd5\u6216\u8054\u7cfb\u7ba1\u7406\u5458\uff01";
}
}
function checkmobile() {
var smobile = getobj("mobile").value;
if (!(/^13[0-9]\d{4,8}$/.test(smobile))) {
return false;
}
return true;
}
function getmobile() {
if (checkmobile()) {
createxhr();
req.open("get", "mobilesearch.asp?mobile=" + getobj("mobile").value, true);
req.send();
req.onreadystatechange = handlestatechange2;
} else {
getobj("disp_mobilearea").innertext = "\u8fd4\u56de\u9519\u8bef\uff1a\u4e0d\u662f\u5b8c\u6574\u768411\u4f4d\u624b\u673a\u53f7\u6216\u8005\u6b63\u786e\u7684\u624b\u673a\u53f7\u524d\u4e03\u4f4d";
}
}
function handlestatechange2() {
if (req.readystate<4){getobj("disp_mobilearea").innertext = "正在查询手机号码["+getobj("mobile").value+"]..."}
else if (req.readystate == 4 && req.status == 200) {
getobj("disp_mobilearea").innertext = req.responsetext;
} else {
getobj("disp_mobilearea").innertext = "\u67e5\u8be2\u8fc7\u7a0b\u51fa\u73b0\u9519\u8bef\uff0c\u8bf7\u91cd\u8bd5\u6216\u8054\u7cfb\u7ba1\u7406\u5458\uff01";
}
}
function chgbtnstyle(){
var btn=document.getelementsbytagname("input")
for(var i=0;i<btn.length;i++){
if(btn[i].type=='button'){
btn[i].classname="btnstyle";
}
}
}