一直在用JS写ASP,也不是特别原因,只是当初学的是JS,后来学ASP时知道ASP也可以用JS写,就没去学VBS.前几个月刚学ASP的时候找遍了深圳的书店也没有找到一本用JS做脚本语言的ASP书籍,没办法,只能自己慢慢摸索着前进,遇到不懂的东西就在网上搜索,在此,发一些JS写ASP与VBS写ASP的区别,希望对刚学JS+ASP的朋友有所帮助.
语法总结: JS语句结束时都要加;号,
写SQL语句要将VBS里面的&号改成+号,
JS中单句注释是//,整段是/*这里是要注释的代码*/,
变量声明是var关键字,不需要写Set,直接var就行,
ASP对象的第一个字母要大写,如Server的S一写要大写,否则会报错.
一些代码的区别
连接数据库
VBS:
<%
dim conn
dim dbpath
dim connstr
Set conn =Server.CreateObject("adodb.connection")
dbpath=Server.MapPath("data.mdb")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&dbpath
conn.open connstr
%>
JS:
<%
var conn;
var dbpath;
var connstr;
conn=Server.CreateObject("adodb.connection");
dbpath=Server.MapPath("data.mdb");
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+dbpath;
conn.open(connstr);
%>
关闭连接
VBS:
<%'关闭释放记录集
rs.close
set rs = Nothing
conn.close
set conn = Nothing
%>
JS:
<%//关闭释放记录集
rs.close;
rs = null;
conn.close;
conn = null;
%>
输出
VBS:
Response.write "<script language='javascript'>alert('操作成功,请等待管理员确认\\点确定关闭窗口');self.opener.location.reload();window.close();</script>"
JS:
Response.write("<script language='javascript'>alert('操作成功,请等待管理员确认\\点确定关闭窗口');self.opener.location.reload();window.close();</script>");
获取表单对象
VBS:
dim id
id = Request.QueryString("id")
JS:
var id = Request.QueryString("id").item;//重要,js中Request.QueryString是一个对象,要加Item
SQL语句日期查询,这个跟VBS的太不相同,相初我郁闷了很久,在网上找了很久才想出下面的语句(这个只写了JS的,VBS的实在不会写)
<%
var rs,sql;
rs = Server.CreateObject("adodb.recordset");
sql = "select * from [count] order by dae desc"
rs.open(sql,conn,1,1)
//实现总访问量
var rst = Server.CreateObject("adodb.recordset");
sqlt="select sum(count) as dt from [count]";
rst.open(sqlt,conn,1,1);
//实现当日访问量
var rsd = Server.CreateObject("adodb.recordset");
sqld="select sum(count) as dd from [count] where day(dae) = '"+((new Date).getDate())+"' and Month(dae) = '"+((new Date).getMonth()+1)+"' and Year(dae) = '"+((new Date).getYear())+"'";
rsd.open(sqld,conn,1,1);
//实现昨日访问量
var rsz = Server.CreateObject("adodb.recordset");
sqlz="select sum(count) as zd from [count] where day(dae) = '"+((new Date).getDate()-1)+"' and Month(dae) = '"+((new Date).getMonth()+1)+"' and Year(dae) = '"+((new Date).getYear())+"'";
rsz.open(sqlz,conn,1,1);
//实现当月访问量
var rsm = Server.CreateObject("adodb.recordset");
sqlm="select sum(count) as dm from [count] where Month(dae) = '"+((new Date).getMonth()+1)+"'";
%>
暂时就想到这么多,顺便说明一下,本人很菜,以上要有不正确之处还望高人指出.
希望版主将此贴置顶,以后相关JS写ASP的问题都可以在此提出.让一些学习JS写ASP的朋友有一个更好的交流空间.
语法总结: JS语句结束时都要加;号,
写SQL语句要将VBS里面的&号改成+号,
JS中单句注释是//,整段是/*这里是要注释的代码*/,
变量声明是var关键字,不需要写Set,直接var就行,
ASP对象的第一个字母要大写,如Server的S一写要大写,否则会报错.
一些代码的区别
连接数据库
VBS:
<%
dim conn
dim dbpath
dim connstr
Set conn =Server.CreateObject("adodb.connection")
dbpath=Server.MapPath("data.mdb")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&dbpath
conn.open connstr
%>
JS:
<%
var conn;
var dbpath;
var connstr;
conn=Server.CreateObject("adodb.connection");
dbpath=Server.MapPath("data.mdb");
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+dbpath;
conn.open(connstr);
%>
关闭连接
VBS:
<%'关闭释放记录集
rs.close
set rs = Nothing
conn.close
set conn = Nothing
%>
JS:
<%//关闭释放记录集
rs.close;
rs = null;
conn.close;
conn = null;
%>
输出
VBS:
Response.write "<script language='javascript'>alert('操作成功,请等待管理员确认\\点确定关闭窗口');self.opener.location.reload();window.close();</script>"
JS:
Response.write("<script language='javascript'>alert('操作成功,请等待管理员确认\\点确定关闭窗口');self.opener.location.reload();window.close();</script>");
获取表单对象
VBS:
dim id
id = Request.QueryString("id")
JS:
var id = Request.QueryString("id").item;//重要,js中Request.QueryString是一个对象,要加Item
SQL语句日期查询,这个跟VBS的太不相同,相初我郁闷了很久,在网上找了很久才想出下面的语句(这个只写了JS的,VBS的实在不会写)
<%
var rs,sql;
rs = Server.CreateObject("adodb.recordset");
sql = "select * from [count] order by dae desc"
rs.open(sql,conn,1,1)
//实现总访问量
var rst = Server.CreateObject("adodb.recordset");
sqlt="select sum(count) as dt from [count]";
rst.open(sqlt,conn,1,1);
//实现当日访问量
var rsd = Server.CreateObject("adodb.recordset");
sqld="select sum(count) as dd from [count] where day(dae) = '"+((new Date).getDate())+"' and Month(dae) = '"+((new Date).getMonth()+1)+"' and Year(dae) = '"+((new Date).getYear())+"'";
rsd.open(sqld,conn,1,1);
//实现昨日访问量
var rsz = Server.CreateObject("adodb.recordset");
sqlz="select sum(count) as zd from [count] where day(dae) = '"+((new Date).getDate()-1)+"' and Month(dae) = '"+((new Date).getMonth()+1)+"' and Year(dae) = '"+((new Date).getYear())+"'";
rsz.open(sqlz,conn,1,1);
//实现当月访问量
var rsm = Server.CreateObject("adodb.recordset");
sqlm="select sum(count) as dm from [count] where Month(dae) = '"+((new Date).getMonth()+1)+"'";
%>
暂时就想到这么多,顺便说明一下,本人很菜,以上要有不正确之处还望高人指出.
希望版主将此贴置顶,以后相关JS写ASP的问题都可以在此提出.让一些学习JS写ASP的朋友有一个更好的交流空间.