龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > Javascript编程 >

ajax登陆框实现弹出层登录效果

时间:2012-12-29 08:41来源:未知 作者:admin 点击:
分享到:
今天在和一客户谈及一个网站时发现的一个弹出层之后用户无刷新登录,这种方法用户体验极好,所以我就把代码拿下来分享一下,有需要的朋友可以参考一下。 代码如下 var xmlHttp; f
今天在和一客户谈及一个网站时发现的一个弹出层之后用户无刷新登录,这种方法用户体验极好,所以我就把代码拿下来分享一下,有需要的朋友可以参考一下。
 代码如下

var xmlHttp;
function createXMLHttpRequest()
{
    try
    {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(sc)
        {
            xmlHttp = null;
        }
    }

    if( !xmlHttp && typeof XMLHttpRequest != "undefined" )
    {
        xmlHttp = new XMLHttpRequest();
    }
    return xmlHttp;
}
function showDiv()
{
    var iWidth = document.documentElement.clientWidth;
    var iHeight = document.documentElement.clientHeight;
    var bgObj = document.getElementById("divbg");
    bgObj.style.cssText = "position:absolute;left:0px;top:0px;width:"+iWidth+"px;height:"+Math.max(document.body.clientHeight, iHeight)+"px;filter:Alpha(Opacity=30);opacity:0.3;background-color:#000000;z-index:1;";

    // 获得事件Event对象,用于兼容IE和FireFox
    function getEvent()
    {
        return window.event || arguments.callee.caller.arguments[0];
    }
    document.getElementById('popDiv').style.display='block';
    document.getElementById('divbg').style.display='block';
}

function closeDiv()
{
    document.getElementById('popDiv').style.display='none';
    document.getElementById('divbg').style.display='none';
}

function readPDF(FDF_ID)
{
    var url = "readpdf.aspx?login=false&id=" + FDF_ID + "&r=" + Math.random();
    start_Login(url);
}
function Subscribe()
{
    var url = "Subscribe.aspx?Subscribe=true&login=false&r=" + Math.random();
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = handleState_Subscribe;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}
function start_Login(url)
{
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = handleState_Login;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}
function handleState_Login()
{
    if(xmlHttp.readyState==4)
    {
        if(xmlHttp.status==200)
        {
            var j = xmlHttp.responseText.parseJSON();
            if(j.islogin == "false")
            {
                alert(j.Reason);
                showDiv();
            }
            else
            {
                //debugger
                closeDiv();
                if(j.exitfile == "true")
                {
                    //alert(j.filepath);
                    top.location.href = "http://" + window.location.host + "/" +j.filepath;
                }
                else
                {
                    if(j.Reason == "")
                    {
                        alert(j.filepath);
                    }
                    else
                    {
                        alert(j.Reason);
                    }
                }
            }
        }
    }  
}

function handleState_Subscribe()
{
    if(xmlHttp.readyState==4)
    {
        if(xmlHttp.status==200)
        {
            var j = xmlHttp.responseText.parseJSON();
            if(j.islogin == "false")
            {
                alert(j.Reason);
                showDiv();
            }
            else
            {
                alert(j.Reason);
                closeDiv();
            }
        }
    }  
}

function pop_Login()
{
    var UserName = document.getElementById("pop_username").value;
    if(UserName.length == 0)
    {
        alert("请输入用户名");
        return false;
    }
    var PassWord = document.getElementById("pop_password").value;
    if(PassWord.length == 0)
    {
        alert("请输入密码");
        return false;
    }
    if(UserName.length > 0 && PassWord.length > 0)
    {
        var url = "readpdf.aspx?Login=true&UserName=" + UserName + "&PassWord=" + PassWord + "&r=" + Math.random();
        start_Login(url);
    }
}

function Subscribe_Login()
{
    var UserName = document.getElementById("pop_username").value;
    if(UserName.length == 0)
    {
        alert("请输入用户名");
        return false;
    }
    var PassWord = document.getElementById("pop_password").value;
    if(PassWord.length == 0)
    {
        alert("请输入密码");
        return false;
    }
    if(UserName.length > 0 && PassWord.length > 0)
    {
        var url = "Subscribe.aspx?Subscribe=true&login=true&UserName=" + UserName + "&PassWord=" + PassWord + "&r=" + Math.random();
        createXMLHttpRequest();
        xmlHttp.onreadystatechange = handleState_Subscribe;
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);
    }
}


调用 方法

 代码如下

<a href="javascript:showDiv();">登录</>就可以了

后面的aspx文件,你只要按正常的登录设置session值再返回1,0状态就好了。


精彩图集

赞助商链接