AJAX 载入JS文件,载入CSS文件,载入JS函数,执行JS 。ASP应用

returnhtm.js

-------------------------------------------------------------------------------------------

var filesadded = ""

function $(o){return document.getElementById(o);}

function XHConn()
{
 var http = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
 
 this.connect = function(element,sURL,sMethod,sVars){
  if(!http){$(element).innerHTML="<img src=images/loading.gif />初始化失败......";return false;}
  sMethod = sMethod.toUpperCase();
  try {
   if (sMethod == "GET") {
    http.open(sMethod, sURL+"?"+sVars, true);
    http.setRequestHeader("Content-Type","text/html;encoding=GB2312");
    sVars = "";
    }else{
    http.open(sMethod, sURL, true);
    http.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
    http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        }
   http.onreadystatechange = function(){
   if (http.readyState == 4){
    if(http.status == 200){
    if(http.responseText!=""){
    var htmlText=http.responseText;
    var DomJs=htmlText.match(/<load-js>[/s/S]+?<//load-js>/g);//获取载入JS文件
    var DomCss=htmlText.match(/<load-css>[/s/S]+?<//load-css>/g);//获取载入CSS文件
    var DomScript=htmlText.match(/<load-script>[/s/S]+?<//load-script>/g);//获取载入函数
    var MyScript=htmlText.match(/<run-script>[/s/S]+?<//run-script>/g);//获取执行语句
    htmlText=htmlText.replace(/<load-js>[/s/S]+?<//load-js>/g,"")//删除载入JS文件文本
    htmlText=htmlText.replace(/<load-css>[/s/S]+?<//load-css>/g,"")//删除载入CSS文件文本
    htmlText=htmlText.replace(/<load-script>[/s/S]+?<//load-script>/g,"")//删除载入函数文本
    htmlText=htmlText.replace(/<run-script>[/s/S]+?<//run-script>/g,"")//删除执行语句文本
    $(element).innerHTML=htmlText;//输出文本
     
    //载入JS文件,以<load-js>开头</load-js>结束
    if(DomJs!=null){
     for(i=0;i<DomJs.length;i++){
      DomJs[i]=DomJs[i].replace(/<load-js>/g,"");
      DomJs[i]=DomJs[i].replace(/<//load-js>/g,"");
       
      if (filesadded.indexOf("["+DomJs[i]+"]")==-1){
       var headerDom=document.getElementsByTagName('head')[0];
       var jsDom=document.createElement('script');
       jsDom.setAttribute("type","text/javascript");
       jsDom.setAttribute("src",DomJs[i]);
       headerDom.appendChild(jsDom);
       filesadded += "["+DomJs[i]+"]";
      }
     }
    }
    //载入CSS文件,以<load-css>开头</load-css>结束
    if(DomCss!=null){
     for(i=0;i<DomCss.length;i++){
      DomCss[i]=DomCss[i].replace(/<load-css>/g,"");
      DomCss[i]=DomCss[i].replace(/<//load-css>/g,"");
       
      if (filesadded.indexOf("["+DomCss[i]+"]")==-1){
       var headerDom=document.getElementsByTagName('head')[0];
       var jsDom=document.createElement('link');
       jsDom.setAttribute("rel", "stylesheet");
       jsDom.setAttribute("type","text/javascript");
       jsDom.setAttribute("href",DomCss[i]);
       headerDom.appendChild(jsDom);
       filesadded += "["+DomCss[i]+"]";
      }
     }
    }
    //载入JS函数,以<load-script>开头</load-script>结束
    if(DomScript!=null){
     for(i=0;i<DomScript.length;i++){
      DomScript[i]=DomScript[i].replace(/<load-script>/g,"");
      DomScript[i]=DomScript[i].replace(/<//load-script>/g,"");
       
      var headerDom=document.getElementsByTagName('head').item[0];
      var jsDom=document.createElement('script');
      jsDom.type='text/javascript';
      jsDom.language='javascript';
      jsDom.defer=true;
      jsDom.text=DomScript[i];
      headerDom.appendChild(jsDom);
     }
    }
    //执行JS,以<run-script>开头</run-script>结束
    if(MyScript!=null){
     for(i=0;i<MyScript.length;i++){
      MyScript[i]=MyScript[i].replace(/<run-script>/g,"");
      MyScript[i]=MyScript[i].replace(/<//run-script>/g,"");
      eval(MyScript[i]);
     }
    }
    }else{$(element).innerHTML="暂无数据......";}
    }else{$(element).innerHTML="你所请求的页面发生异常!返回:"+http.status;}
   }//else{$(element).innerHTML="<img src=images/loading.gif />载入数据中......";}
   };
   http.send(sVars);
     }
     catch(z) { return false; }
     return true;
 };
return this;
}

function getFormData(formname) {
    var dataString = "";

    function addParam(name, value) {
        dataString += (dataString.length > 0 ? "&" : "")
   + escape(name).replace(//+/g, "%2B") + "="
            + encodeURIComponent(escape(value ? value : ""));
    }
 
    var elemArray = $(formname).elements;
    for (var i = 0; i < elemArray.length; i++) {
        var element = elemArray[i];
        var elemType = element.type.toUpperCase();
        var elemName = element.name;
  var elemId = element.id;
        if (elemName) {
            if (elemType == "TEXT"
                    || elemType == "TEXTAREA"
                    || elemType == "PASSWORD"
                    || elemType == "HIDDEN")
                addParam(elemName, element.value);
            else if (elemType == "CHECKBOX" && element.checked)
                addParam(elemName, element.value ? element.value : "On");
            else if (elemType == "RADIO" && element.checked)
                addParam(elemName, element.value);
            else if (elemType.indexOf("SELECT") != -1)
                for (var j = 0; j < element.options.length; j++) {
                    var option = element.options[j];
                    if (option.selected)
                        addParam(elemName,
                            option.value ? option.value : option.text);
                }
        }else if(elemId){
            if (elemType == "TEXT"
                    || elemType == "TEXTAREA"
                    || elemType == "PASSWORD"
                    || elemType == "HIDDEN")
                addParam(elemId, element.value);
            else if (elemType == "CHECKBOX" && element.checked)
                addParam(elemId, element.value ? element.value : "On");
            else if (elemType == "RADIO" && element.checked)
                addParam(elemId, element.value);
            else if (elemType.indexOf("SELECT") != -1)
                for (var j = 0; j < element.options.length; j++) {
                    var option = element.options[j];
                    if (option.selected)
                        addParam(elemId,
                            option.value ? option.value : option.text);
                }
  }
    }
 return dataString;
}

//DIV名,交互页面,GET or POST,参数,表单名

function htmlupdate(element,url,method,data,formname){
 var xh = new XHConn();
 if($(formname)){data+="&"+getFormData(formname);}
 xh.connect(element,url,method,data);
}

-------------------------------------------------------------------------------------

ASP调用

<script language="JavaScript" src="returnhtm.js" type="text/javascript"></script>

 

<script>htmlupdate('reg_main','UserCheck.asp','GET','cmd=1');</script>

 

<input type="button" value="创建用户帐户" οnclick="this.disabled=true;htmlupdate('temp','/UserCheck.asp','GET','cmd=3','user_reg');this.disabled=false;" name="sbfrm"/>

 

<input name="UserName" type="text" class="myinput" id="UserName" onBlur="JavaScript:htmlupdate('user_show','/UserCheck.asp','POST','cmd=2','user_reg');" />

-------------------------------------------------------------------------------------

 

UserCheck.asp

-------------------------------------------------------------------------------------

<%
Response.Buffer = True
Response.ExpiresAbsolute = Now() - 1
Response.Expires = 0
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "No-Cache"
Response.CharSet = "GB2312"

cmd=cint(request("cmd"))

Select Case cmd
Case 1

..........................................

Case 2

 UserName=trim(unescape(request("UserName")))
 if RegExpTest("^[@./-_A-Za-z0-9/u4E00-/u9FA5]{4,20}$",UserName)=false then
  response.write "<img src=/images/ERROR.GIF /> <font color=#FF0000>由4-20位的中英文与字符@.-_组成!</font>"
 else
  sql="select * from dv_user where username='"&UserName&"'"
  set rs=conn.execute(sql)
  if not rs.eof then
   Response.write "<img src=/images/ERROR.GIF /> <font color=#FF0000>该帐号已经存在!</font>"
  else
   Response.write "<img src=/images/RIGHT.GIF />"
  end if
  set rs=nothing
 end if 
Case 3

 UserName=trim(unescape(request("UserName")))
 if RegExpTest("^[@./-_A-Za-z0-9/u4E00-/u9FA5]{4,20}$",UserName)=false then
  response.write "<run-script>alert('用户帐号由4-20个英文字母或数字、中文、字符@.-_等组成!');$('UserName').focus();</run-script>"
  response.end
 else
  sql="select * from dv_user where username='"&UserName&"'"
  set rs=conn.execute(sql)
  if not rs.eof then
   Response.write "<run-script>alert('该帐号已经存在!');$('UserName').focus();</run-script>"
   response.end
  end if
  set rs=nothing
 end if 
 nickname=trim(unescape(request("nickname")))
 if RegExpTest("^[A-Za-z0-9/u4E00-/u9FA5]{1,20}$",nickname)=false then
  Response.write "<run-script>alert('昵称必须是1-20位的中英文组成!');$('nickname').focus();</run-script>"
  response.end
 end if 
 UserPassword=trim(unescape(request("UserPassword")))
 if RegExpTest("^[A-Za-z0-9]{4,20}$",UserPassword)=false then
  Response.write "<run-script>alert('密码由4-20个英文字母或数字组成!');$('UserPassword').focus();</run-script>"
  response.end
 end if 
 UserPassword1=trim(unescape(request("UserPassword1")))
 if UserPassword<>UserPassword1 then
  Response.write "<run-script>alert('您两次输入的密码不一致,请确认!');$('UserPassword1').focus();</run-script>"
  response.end
 end if 
 UserEmail=trim(unescape(request("UserEmail")))
 if RegExpTest("^([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[/.][a-z]{2,3}([/.][a-z]{2})?$",UserEmail)=false then
  Response.write "<run-script>alert('Email地址格式不正确!');$('UserEmail').focus();</run-script>"
  response.end
 end if 
 getcode=trim(unescape(request("getcode")))
 if getcode<>Trim(Session("getCode")) then
  Response.write "<run-script>alert('验证码错误!');$('getcode').focus();</run-script>"
  response.end
 end if 
 
 session.contents.remove("getCode")
 
 UserFace=Trim(unescape(request("UserFace")))
 UserWidth=Trim(unescape(request("UserWidth")))
 UserHeight=Trim(unescape(request("UserHeight")))
 UserIM=Trim(unescape(request("UserIM")))
 UserClass=Trim(unescape(request("UserClass")))
 userWealth=Trim(unescape(request("userWealth")))
 userEP=Trim(unescape(request("userEP")))
 userCP=Trim(unescape(request("userCP")))
 UserFav=Trim(unescape(request("UserFav")))
 UserInfo=Trim(unescape(request("UserInfo")))
 UserSetting=Trim(unescape(request("UserSetting")))
 UserGroupID=Trim(unescape(request("UserGroupID")))
 TitlePic=Trim(unescape(request("TitlePic")))
 TruePassWord=Trim(unescape(request("TruePassWord")))
 UserToday=Trim(unescape(request("UserToday")))
 CommID=Trim(unescape(request("CommID")))

 sql="set nocount on;insert into Dv_User (username,UserPassword,UserEmail,UserFace,UserWidth,UserHeight,UserIM,UserClass,userWealth,userEP,userCP,UserFav,UserInfo,UserSetting,UserGroupID,TitlePic,TruePassWord,UserToday,c_id) values ('"&username&"','"&md5(md5(UserPassword,16),32)&"','"&UserEmail&"','"&UserFace&"',"&UserWidth&","&UserHeight&",'"&UserIM&"','"&UserClass&"',"&userWealth&","&userEP&","&userCP&",'"&UserFav&"','"&nickname&UserInfo&"','"&UserSetting&"',"&UserGroupID&",'"&TitlePic&"','"&TruePassWord&"','"&UserToday&"',"&CommID&");select @@identity as newid;SET NOCOUNT OFF"
 set rs=conn.execute(sql)
 'Response.Cookies("user_userid")=rs("newid")
 'Response.Cookies("user_username")=UserName
 'Response.Cookies("user_pass")=md5(md5(now(),16),32)
 set rs=nothing
 Response.Write "<run-script>location.href='my_user.asp?CommID="&CommID&"';</run-script>"

End Select

 

'正则表表达式验证函数 patrn-正则表达式 strng-需要验证的字符串
Function RegExpTest(patrn, strng)
Dim regEx, retVal ' 建立变量。
Set regEx = New RegExp ' 建立正则表达式。
regEx.Pattern = patrn ' 设置模式。
regEx.IgnoreCase = False ' 设置是否区分大小写。
retVal = regEx.Test(strng) ' 执行搜索测试。
RegExpTest = retVal '返回不尔值,不符合就返回false,符合为true
End Function

%>

-------------------------------------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值