Ajax学习笔记

来源:互联网 发布:ff14人男捏脸数据 编辑:程序博客网 时间:2024/05/01 06:14

Ajax技术应该是时下Web开发最火的技术了吧,,减轻了服务器的处理量,实现无刷新重载来增强了用户体验。最大限度的减少对服务器的冗余请求...........................

      今天又看到了baidu利用Ajax技术处理跨界的新方法,确实令我眼前一亮,下面是一段baidu的脚本文件:

 

<SCRIPT LANGUAGE="JavaScript">
document.domain="baidu.com";
<!--
function G(id){if(typeof(id)=="string"){return document.getElementById(id);}return id;}
function showInfo(obj){
    if(obj.checked == true){
        G("memInfo").style.display="block";
    }else{
        G("memInfo").style.display="none";
    }
}
function request(id,url){
    oScript = document.getElementById(id);
    var head = document.getElementsByTagName("head").item(0);
    if (oScript) {
        head.removeChild(oScript);
    }
    oScript = document.createElement("script");
    oScript.setAttribute("src", url);
    oScript.setAttribute("id",id);
    oScript.setAttribute("type","text/javascript");
    oScript.setAttribute("language","javascript");
    head.appendChild(oScript);
    return oScript;
}
var loginTimer=null;
var loginState=-1;
var tryTime=0;
function PSP_ik(isOk){
    if(isOk==0){
        G("errorInfo").style.display="none";
        loginState=1;
        if(parent.loginSuccess){
            parent.Pop.hide();
            parent.loginSuccess();
        }
    }
    else
    {
        loginFalse();
    }
}

function loginFalse(){
    loginState=0;
    var err=G("errorInfo");
    err.innerHTML="用户名或密码错误,请重新登录";
    err.style.display="block";
    G("username").focus();
    tryTime++;
    if(tryTime>1){
        onLoginFailed();
    }
}
function onLoginFailed(){
    if(parent.onLoginFailed){
        parent.Pop.hide();
        parent.loginFailed();
    }else{
        document.login.u.value=escape("http://zhidao.baidu.com/q"+parent.location.search);
        doucment.login.submit();
    }
       
}
function loginTimeout(){
    if(loginState==-1){
        var err=G("errorInfo");
        err.innerHTML="操作超时,请重新登录";
        err.style.display="block";
        G("username").focus();
    }
}
function userLogin(){
    var username=G('username').value;
    var password=G('password').value;
    var memPassport=G('memPassport').checked?"on":"off";
    if(username.length <=0||password.length <=0){G("username").focus();return false;}
    var url = 'https://passport.baidu.com/?logt&tpl=ik&t=0&keyname=ik&mem_pass='+memPassport+'&username='+username + '&loginpass=' +escape(password)+ '&s=' + (new Date()).getTime();
    loginState=-1;
    var login=request("loginScript",url);
    loginTimer = setTimeout(loginTimeout, 5000);

}
window.onload=function(){
    document.loginForm.username.focus();   
    document.getElementById("username").focus();
}
//-->
</SCRIPT>

 

我一般都会用xmlhttp发送get请求实现,但百度用的是request方法处理异步请求使用动态往head中添加script,当页面加载后,可以达到相同的效果,同时完美地解决了跨界的问题,确实很精妙。