跟我学cookies 1
varstr1=text.substring(0,3);
varstr2=text.substring(3,6);
运行该段代码后变量str1的值为"her";变量tool的值为"mes"。 本文来自织梦
子字符串常和indexOf一起使用,将字符串分成若干块.例如, 织梦好,好织梦
你可以从一个给定的URL中抽取出其域名: 织梦内容管理系统
varthe_url=prompt("What'stheURL?","");
varlead_num=the_url.indexOf("//"); 确定第一个双斜杠的位置
vardomainloc=lead_num 2; 域名开始位置
varnolead=the_url.substring(domainloc,the_url.length); http://后面的字符串
varnext_num=nolead.indexOf("/"); /的位置
vardomain=without_resource.substring(0,next_num); 提取域名
这段代码的意思是:假如你输入
"../index.htm",则域名domain的值就是"www.hermes.com.cn".
假如这个方法对你来说有些麻烦,我将向你介绍如何使用split方法简化其执行过程.
本文来自织梦
4.split(分割)
split方法可以分割一系列的字符串,然后将其放在一个数组中.例如: 织梦内容管理系统
varanimal="dog,monkey,fox,rabbit,pig";
varanimal_array=animal.split(",");
for(loop=0;loop<animal_array.length;loop )
{
document.writeln(animal_array[loop] "isverylovely.
");
} dedecms.com
这段代码将字符串animal分割成包含5个元素的数组.JavaScript可以为你自动建立一个数组,所以你无需使用newArray().
将字符串分割成数组之后,我们使用了循环语句写出每一个名称.我们可以利用split方法简化前面所讲到的域名提取:
织梦内容管理系统
varthe_url=prompt("What'stheURL?","");
varfirst_split=the_url.split("//");
varlast_split=first_split[1];
varsecond_split=last_split.split("/");
vardomain=second_split[0];
我们来分析一些这段代码:
varthe_url=prompt("What'stheURL?",""); 内容来自dedecms
提示用户输入一个URL,假设用户输入 "../index.htm".
varfirst_split=the_url.split("//");
将用户输入的字符串分割成两块:first_split[0]是"http:",
first_split[1]是"www.hermes.com.cn/new/index.htm" dedecms.com
varlast_split=first_split[1];
提取出数组中的第2个元素,所以现在last_split是
"www.hermes.com.cn/new/index.htm" 本文来自织梦
varsecond_split=last_split.split("/");
将last_split分割成3块:www.hermes.com,new,和index.htm.并且自动生成以这3块为元素的second_split数组.
内容来自dedecms
vardomain=second_split[0];
取出新数组中的第1个元素得出域名. 织梦好,好织梦
也许你有些烦了:怎么这么罗嗦,我要学cookies,你却先说了这么一大堆!
"万丈高楼平地起" 嘛,好了,我们下一课就开始神奇的cookies之旅吧. 织梦好,好织梦
dedecms.com
文章评论
共有位Admini5网友发表了评论 查看完整内容