常用JavaScript脚本

1.透明Flash代码

<param name="wmode" value="transparent">

2.加入收藏

<a href="JavaScript:window.external.addfavorite(‘http://www.yongfa365.com’,'柳永法(yongfa365)\’Blog’)" target="_top">加入收藏</a>

3.设为首页

<a href="#" target="_self"   style="cursor:hand" onclick=this.style.behavior="url(#default#homepage)";this.sethomepage("http://www.yongfa365.com")>设为首页</a>

4.不同时间段显示不同问候语

   <Script Language="JavaScript">
   <!–
   var text=""; day = new Date( ); time = day.getHours( );
   if (( time>=0) && (time < 7 ))
     text="夜猫子,要注意身体哦! "
   if (( time >= 7 ) && (time < 12))
     text="今天天气……哈哈哈,不去玩吗?"
   if (( time >= 12) && (time < 14))
     text="午休时间哦,朋友一定是不习惯午睡的吧?!"
   if (( time >=14) && (time < 18))
     text="下午茶的时间到了,休息一下吧! "
   if ((time >= 18) && (time <= 22))
     text="您又来了,可别和MM聊太久哦!"
   if ((time >= 22) && (time < 24))
     text="很晚了哦,注意休息呀!"
   document.write(text)
   //—>
   </Script>

5.改变IE地址栏的IE图标
我们要先做一个16*16的icon(图标文件),保存为index.ico。把这个图标文件上传到根目录下并在首页之间加上如下代码:

<link REL = "Shortcut Icon" href="index.ico">

6.2秒后载入指定网页

   <head>
   <meta http-equiv="refresh" content="2;URL=http://www.yongfa365.com/blog">
   </head>

7.显示当前日期,星期

<Script Language="JavaScript">
<!–
var today=new Date();
var strDate=(" "+today.getYear()+"年"+(today.getMonth()+1)+"月"+today.getDate()+"日");
var n_day=today.getDay();switch(n_day)
{case 0:
{strDate=strDate+"(星期日)"}break;
case 1:
{strDate=strDate+"(星期一)"}break;
case 2:
{strDate=strDate+"(星期二)"}break;
case 3:
{strDate=strDate+"(星期三)"}break;
case 4:
{strDate=strDate+"(星期四)"}break;
case 5:
{strDate=strDate+"(星期五)"}break;
case 6:
{strDate=strDate+"(星期六)"}break;
case 7:
{strDate=strDate+"(星期日)"}break;
}
document.write(strDate);
//   –>
</Script>

8.滚动代码

<marquee direction=up scrolldelay=200   scrollamount=1 width=206 height=207   onmouseover=this.stop() onmouseout=this.start()>大家好</marquee>

9.这个比较常用,一般用来处理长串连续英文撑破表格的问题

<td style="word-break:break-all">

10.图片渐渐显示出来

<SCRIPT language="JavaScript">
<!–
function makevisible(cur,which)
{
if (which==0)
cur.filters.alpha.opacity=100
else
cur.filters.alpha.opacity=50
}
//–>
</SCRIPT>

<img src="yongfa.gif" style="filter:alpha(opacity=20)" onMouseOver="makevisible(this,0)" onMouseOut="makevisible(this,1)">

11.删除文件

<%
set fso=server.CreateObject("scripting.filesystemobject")
filename1=server.MapPath("Pic")
if fso.fileexists(filename1) then
fso.deletefile filename1
end if
%>

12.mailto

<a href=mailto:123456789@163.com;heroyongfa@163.com?cc=12345@163.com&bcc=1234567890@sina.com&subject=您做的网页不错呀&body=再接再厉>E-mail</a>

13.内容显示or隐藏

<script language="JavaScript">
function show(i){
if (i.style.display == "none") {
   i.style.display = "";
}else{
   i.style.display = "none";
}
}
</script>

<div onClick="show(a1)"   style="cursor:hand" >标题</div>
<DIV id=a1 style="DISPLAY: none">内容</DIV>

14.Window.open

window.open (‘page.html’, ‘newwindow’, ‘height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no’)

15.两次确认是否删除(也不知这客户是怎么了)

<input type="button" name="Submit2" value="删除" onclick=" javasctipt:if (window.confirm(‘确定删除吗?’)){if (window.confirm(‘确定删除吗?’)){window.location.href=’?action=del&id=<%=rs("id")%>’}}" >

16.按钮版确定删除吗?

<input type="button" name="Submit5" value="删除" onclick="if (confirm(‘您确定要删除嘛?’)){window.location.href=’?action=delok&id=<%=rs("id")%>’;}" />

17.分类全选记录的函数

演示代码,只要checkbox的name一样就可以
调用时用checkall(this,’checkboxname’)就可以

<script language=JavaScript>
function checkall(all,checkname)//用于判断全选记录的函数
{
var a = document.getElementsByName(checkname);
for (var i=0; i<a.length; i++) a[i].checked = all.checked;
}
</script>

<input type="checkbox" name="chkall" value="on" onClick="checkall(this,’question’)">

18.打印本页

window.print()

19.引用upload.html iframe
以下为编辑器的简单调用

添加:
<input type="hidden" name="content1" value="">
<iframe ID="eWebEditor1" src="../ewebeditor.asp?id=content1&style=standard" frameborder="0" scrolling="no" width="500" HEIGHT="350"></iframe>
修改:
<input type="hidden" name="content1" value="<%=Server.HTMLEncode(Rs("Content"))%>">
<iframe ID="eWebEditor1" src="../ewebeditor.asp?id=content1&style=standard" frameborder="0" scrolling="no" width="500" HEIGHT="350"></iframe>

21.插入Flash

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="684" height="316">
           <param name="movie" value="images/111.swf">
           <param name="wmode" value="transparent">
           <param name="quality" value="high">
           <embed src="images/111.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="684" height="316"></embed>
      </object>

22.select改变时 跳转或执行操作

天气:<input name="weather" type="text" id="weather" size="5">
<select name="selectweather" onChange="weather.value=this.options[selectedIndex].value">
<option value="天气">天气</option>
<option value="睛">睛</option>
<option value="阴">阴</option>
<option value="小雨">小雨</option>
<option value="多云">多云</option>
<option value="阵雨">阵雨</option>
<option value="连阴">连阴</option>
<option value="小雪">小雪</option>
</select>

23.应付框架里登录的问题

‘把下边这个放在登录页面head里

<script language="JavaScript">
if (self!=top){top.location=’admin_login.asp’;}
</script>

‘或这样写

response.redirect " <script language=’JavaScript’>top.location=’login.asp’</script>"

24.Ctrl+Enter提交表单

<textarea name="co" cols="50" rows="10" onKeyDown="ctlSubmit(event);"   style="width:97%;"></textarea>

<script>
function ctlSubmit(event) {
if (event.ctrlKey && event.keyCode == 13) { form1.submit() }
}
</script>

25.只能输入数字且数字要大于3

<script>
function min_in()
{
if (event.keyCode < 45 || event.keyCode > 57){alert(‘同志只能输入数字,谢谢’); event.returnValue = false;}
}

function check_input(aaa)
{
if (aaa!=”&&aaa<3)
{
alert("哥哥,太少了");
return false;
}
}
</script>
<input type="text" name="money" value="" onKeypress="return min_in()" onblur="check_input(this.value)" >

26.全选与反选

function CheckOthers(form)
{
for (var i=0;i<form.elements.length;i++)
{
   var e = form.elements[i];
    if (e.checked==false)
    {
     e.checked = true;
    }
    else
    {
     e.checked = false;
    }
}
}

function CheckAll(form)
{
for (var i=0;i<form.elements.length;i++)
{
   var e = form.elements[i];
    e.checked = true;
}
}

30.改变obj的class属性值

<head>
   <style>
   .aa{color:red}
   .bb{color:blue}
   </style>
   </head>
   <body>
   <form    name=form1>
   <input    name=txt1    value="测试"    class="aa">
   <input    type="button"    onclick="form1.txt1.className=’bb’"    value="改变">
   </form>
   </dody>

31.如何在标题栏显示框架内页面的标题

top.document.title = document.title

32.脚本永不出错

<SCRIPT LANGUAGE="JavaScript">
<!– Hide
function killErrors() {
return true;
}
window.onerror = killErrors;
// –>
</SCRIPT>

33.让输入框关闭自动完成(AutoComplete)功能

<form>
让输入框关闭自动完成(AutoComplete)功能<br />
测试方法:分别双击两个文本框看看有什么不一样<br />
默认:<input name="UserName" type="text" /><br />
关闭:<input name="UserName" type="text" autocomplete="off" /><br />
</form>

34.匹配所有没有alt属性的img的正则表达式

<img(?!.*?alt)(.*?)>

35.得到字符串的中文|英文|数字|全部长度,返回值为字节数

//得到字符串的中文|英文|数字|全部长度,返回值为字节数
//strtype="cn"|"en"|"num"|"all"

function GetStrNum(str, strtype)
{
    var ch, num_cn = 0, num_en = 0, num_num = 0, num_other = 0;
    for (var i = 0; i < str.length; i++)
    {
        ch = str.substr(i, 1);
        if (/[^\x00-\xff]/.test(ch))
        {num_cn += 2;}
        else if (/\w/.test(ch))
        {num_en += 1;}
        else if (/\d/.test(ch))
        {num_num += 1;}
        else
        {num_other += 1;}
    }
    switch (strtype)
    {
        case "cn":
            return num_cn;
        case "en":
            return num_en;
        case "num":
            return num_num;
        default:
            return num_cn + num_en + num_num + num_other;
    }
}

36.得到一字符串的长度最短的代码

"我i".replace(/[^\x00-\xFF]/g,"aa").length
您可以直接在URL里输入 : javascript:alert("我i".replace(/[^\x00-\xFF]/g,"aa").length)

37.当别人Copy你的文章时,自动在文章后边加上 转自:。。。。。

document.body.oncopy = function () { setTimeout( function () { var text = clipboardData.getData("text"); if (text) { text = text + "\n  转自:" + location.href; clipboardData.setData("text", text); } }, 100 ) }

39.消除图像工具栏,鼠标放在网页上的图片上时会显示一个保存图像的工具栏,怎么去掉?

单个图片可以这样:
<img src="http://www.baidu.com/img/logo.gif" galleryimg="false">
如果想让网页内所有图片都不出现工具栏,可以用下边这个
<head>
<meta http-equiv="imagetoolbar" content="no">
</head>

40.哪果您的网页编码是XHTML 1.0那么,你得:

document.body换成document.documentElement,或者您可以先判断一下:

if (document.documentElement)
objBody = document.documentElement
else if (document.body)
objBody = document.body
else
    {/*Netscape stuff*/}

41.网页屏幕相关属性测试

<body>
<script language="javascript">
var s = "";
s += "\r\n网页可见区域宽:"+ document.body.clientWidth;
s += "\r\n网页可见区域高:"+ document.body.clientHeight;
s += "\r\n网页可见区域宽:"+ document.body.offsetWidth +" (包括边线和滚动条的宽)";
s += "\r\n网页可见区域高:"+ document.body.offsetHeight +" (包括边线的宽)";
s += "\r\n网页正文全文宽:"+ document.body.scrollWidth;
s += "\r\n网页正文全文高:"+ document.body.scrollHeight;
s += "\r\n网页被卷去的高:"+ document.body.scrollTop;
s += "\r\n网页被卷去的左:"+ document.body.scrollLeft;
s += "\r\n网页正文部分上:"+ window.screenTop;
s += "\r\n网页正文部分左:"+ window.screenLeft;
s += "\r\n屏幕分辨率的高:"+ window.screen.height;
s += "\r\n屏幕分辨率的宽:"+ window.screen.width;
s += "\r\n屏幕可用工作区高度:"+ window.screen.availHeight;
s += "\r\n屏幕可用工作区宽度:"+ window.screen.availWidth;
s += "\r\n你的屏幕设置是 "+ window.screen.colorDepth +" 位彩色";
s += "\r\n你的屏幕设置 "+ window.screen.deviceXDPI +" 像素/英寸";
alert(s);
</script>
</body>