第一个应用:
IMG的onerror自动选择最快线路,根据服务器返回 错误。
思路:
我们在www.xxx.com 中让客户端同时向地址dianxin.xxx.com和wangtong.xxx.com发出一个错误的请求,哪一个地址返回错误的响应速度快就说明用户应该访问那个地址。
关键代码:
<img src="http://dianxin.xxx.com/NotExistsUrl" width="1" height="1"
onerror="location.top.url=’http://dianxin.xxx.com/’"/>
<img src="http://wangtong.xxx.com/NotExistsUrl" width="1" height="1"
onerror="location.top.url=’http://wangtong.xxx.com/’"/>
为了在用户下次访问的时候可以直接跳转到比较快的线路上我们可以将这一次判断的结果保存到cookie中。
第二个应用:
IMG的onerror事件的另一个用武之地:
我们都不希望用户看到我们网站上面出现无效的图片,即便是出现了无效的图片我们也希望以一种友好的方式告诉用户图片无效,而不是直接给用户看默认的红叉;怎么办,我们可以在图片的onerror事件中将图片的src属性设置为我们网站上已存在的表示图片不存在的图片。
代码示例:
<img src="http://www.xxx.com/someImage.gif"
onerror="this.src=’http://www.xxx.com/invlid.gif’"/>
检测某个网站的链接速度:
<script language=javascript>
/********
i=1
var autourl=new Array()
autourl[1]="http://www.asp50.com"
autourl[2]="http://asp.cn.st"
function auto2(url){if(i){i=0;top.location=url}}
function run2(){
for(var i=1;i < autourl.length;i++)
{
document.write("<img src="+autourl[i]+" width=1 height=1 onerror=auto2(‘"+autourl[i]+"’)>")
}
}
run2()
**********/
tim=1//十分之一秒
setInterval("tim++",100)
b=1
var autourl=new Array()
autourl[1]="www.njcatv.net"
autourl[2]="www.qcomdd.com"
autourl[3]="www.sina.com.cn"
autourl[4]="www.nuaa.edu.cn"
autourl[5]="www.cctv.com"
function butt(){
document.write("<form name=autof>")
for(var i=1;i<autourl.length;i++){
document.write("<input type=text name=txt"+i+" size=10 value=测试中……> =》<input type=text name=url"+i+" size=40> =》<input type=button value=GO onclick=window.open(this.form.url"+i+".value)><br />")}
document.write("<input type=submit value=重新测试></form>")
}
butt()
function auto(url){
document.forms[0]["url"+b].value=url
if(tim>200)//20秒
{
document.forms[0]["txt"+b].value="链接超时"}
else
{
document.forms[0]["txt"+b].value="时间"+tim/10+"秒"}
b++
}
function run(){
for(var i=1;i<autourl.length;i++){
document.write("<img src=http://"+autourl[i]+"/"+Math.random()+" width=1 height=1 onerror=auto(‘http://"+autourl[i]+"’)>")
}
}
run()
</script>
<script language="JavaScript">
var str="test!"
document.write(str.link("#"))
document.links[0].href="http://www.xueit.com";
document.links[0].title="青青";
document.write(document.links.length);
</script>
<pre />
str
属性
length 字符串中的字符个数
方法
anchor() 用来把字符串转换到HTML锚点标记内(<A NAME=>)
big() 把字符串中的文本变成大字体(<BIG>)
blink() 把字符串中的文本变成闪烁字体(<BLINK>)
bold() 把字符串中的文本变成黑字体(<B>)
charAt() 寻找字符串中指定位置的一个字符
fixed() 把字符串中的文本变成固定间距字体(<TT>)
fontcolor() 改变字符串中文本的颜色(<FONT COLOR=>)
fontsize() 把字符串中的文本变成指定大小(<FONTSIZE=>)
indexOf() 用来搜索字符串中的某个特殊字符,并返回该字符的索引位置
italics() 把字符串中的文本变成斜字体(<I>)
lastlndexOf() 与indexof相似,但是向后搜索最后一个出现的字符
small() 把字符串中的文本变成小字体(<SMALL>)
strike() 把字符串中的文本变成划掉字体(<STRIKE>)
sub() 把字符串中的文本变成下标(subs cript)字体((SUB>)
substring() 返回字符串里指定位置间的一部分字符串
link() 用来把字符串转换到HTML链接标记中(<A HREF=>)
利用图片img的onerror两种应用例子(自动选择最快服务器)
2010-04-02
龙,good!!<img src=”/image/face/30.gif” />
博主的创意高啊