语法规则如下:
1、 每一行程序均以分号结尾
2、 注释方法:“//”或者“/* */”
3、 使用关键字“var”来声明变量
怎么样?从上面我们就可看出来,wmlscript语言和javascript语言是多么的相似,wmlscript的变量类型有:boolean、interger、floating-point、string、invalid wmlscript的流程控制语言和javascript的相同,实例如下:
// if-else
if (x == y){ x = x * 3.25; }else{ x = 0; }
// for loop
for (var counter = 1; counter < 500; counter ++){ var i = counter * 1.05; somefunction(i); };
// while loop
while (i > j){ i--; };
// break
for (var counter = 1; counter < 500; counter ++){ var i = counter * 1.05; if (counter == 250) break; somefunction(i); };
file://continue
for (var counter = -100; counter < 100; counter ++){ if (counter == 0) continue; var x = 350/counter; };
函数:
wmlscript的函数使用方法如下:
extern function identifier(formatparameterlist) block ; extern是说明函数是放在另外一个文件当中的。如下:
function runtime(distance, speed){ var time = distance / speed; return time; };
此例出入distance以及speed两个参数,然后返回time的值。如果调用的是wmlscript的內建函数,就必须加上这个函数所属的类名,例如调用 string类的length()函数,使用: var a = string.length('just a test'); 內建函数。
wmlscript有六大內建函数:
lang:包含数据形态、绝对值、随机数等。
float:浮点数处理。
string:字符串长度(length)、字符位置(charat)等字串处理。
url:getreferer、gethost。
wmlbrowser:go、prev、next、refresh等浏览处理。
dialogs:弹出(prompt、confirm、alert)等对话框。
首先我们先建立一个wml文件,wmlscriptexample.wml,內容如下:
<?xml version="1.0"?>
<!doctype wml public "-//wapforum//dtd wml 1.1//en"
"http://www.wapforum.org/dtd/wml_1.1.xml">
<wml>
<card id="stuid" title="stuid:">
<do type="accept" label="results">
<go href="wmlscriptexample.wmls#checkid($(stuid))"/>
</do>
<p>
enter stu_id: <input type="text" name="stuid"/>
</p>
</card>
<card id="results" title="results:">
<p>
you entered:<br/>
stu_id: $(stuid)<br/>
</p>
</card>
</wml>
让用户输入学号,go的href连接到另外一个.wmls文件,调用checkid函数。再建 立一个wmlscriptexample.wmls文件,內容如下:
extern function checkid(stuid){ if (string.length(stuid) != 8){ wmlbrowser.setvar("stuid", "error: string must be 8 digits long."); } wmlbrowser.go("wmlscriptexample.wml#results"); };
这个wmlscript就是检查stuid字串长度是否为8,并且返回结果。 wmlbrowser.setvar()设定变量的值,wmlbrowser.go()將手机定位到下一个 wml文件,也就是wmlscriptexample.wml的results卡片。
怎么样?wmlscript是不是很类似于javascript?简直就是一模 一样,就是多了和wml文件相配合的功能而已。
最后需要指出的是,现在的大多数wap手机(nokia7110、爱立信r320s等等)都是使用utf-8编码的,也就是采用uniecode来编码,这样,如果我们直接在wml/wmlscript中使用中文的话,将会产生乱码,手机用户无法识别,所以我们在输出中文之前,要对中文进行uniecode的编码,这个工作就要服务器端的程序来完成了,比如我们使用asp来做这项工作,可以调用这个函数实现转换,其他语言的实现方法类似:
function c2u(mytext)
for i = 1 to len(mytext) ' 对每一个字节进行处理
c = mid(mytext, i, 1)
c2u = c2u & "&#x" & hex(ascw(c)) & ";"
' 将传回的unicode转换为十六进制值
next
end function
实现的例子如下:(chinese.asp)
<% response.contenttype="text/vnd.wap.wml;charset=utf-8"
%> <?xml version="1.0"?>
<!doctype wml public "-//wapforum//dtd wml 1.1//en"
"http://www.wapforum.org/dtd/wml_1.1.xml">
<% function c2u(mytext)
for i = 1 to len(mytext)
c = mid(mytext, i, 1)
c2u = c2u & "&#x" & hex(ascw(c)) & ";"
next
end function
%> <wml>
<card id="card1" title="utf8">
<p>
<%=c2u("it的事找赛迪网!!")%><br/>
</p>
</card>
</wml>
在诺基亚的仿真手机中浏览这个asp文件,就可以看到中文了!
结束语
我们通过上面的介绍,也可以大致了解一下wap技术的来龙去脉了,相信通过你自己的不断实践,一定能够驾驭它的。其实wml和html一样属于一个静态的页面,要 想真正建设吸引用户的wap站点,不光要仅仅依靠wml本身,还要配合各种运行在服务器端的程序,比如asp、php、jsp、cgi等等来实现丰富多彩的动态wap网站,这就是后话了,暂且不谈。随着国内的wap技术大环境的越来越完善,将会有更多的网站、isp等投入到这场抢夺市场的战斗中来,相信wap技术的可移动性和简易操作性将会吸引越来越多的用户投身到广阔的无线网络中,对于我们web开发人员来讲,掌握了wap就掌握了未来。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器