常用js函数收集

1.去除字符串首尾空格 (拷过去,就直接可以用.trim()函数)  
------------------------------------------------------------  
String.prototype.trim = function()
{
            return this.replace(/(^/s+)|(/s+$)/g, "");
}


------------------------------------------------------------  
2.禁止另存为  
------------------------------------------------------------  
<NOSCRIPT><IFRAME SRC=*.html></IFRAME></NOSCRIPT>  

------------------------------------------------------------  
3.为网页设置背景音乐  
------------------------------------------------------------
利用JS来完成,可以兼容Opera和Netscape

<script language="JavaScript">  
<!-- Begin  

var MSIE=navigator.userAgent.indexOf("MSIE";  

var NETS=navigator.userAgent.indexOf("Netscape";  

var OPER=navigator.userAgent.indexOf("Opera";  

if((MSIE>-1) ││ (OPER>-1)) {  

document.write("<BGSOUND SRC=/"birth.mid/" LOOP=INFINITE>";  

} else {  

document.write("<EMBED SRC=/"birth.mid/" width=2 height=1 loop = true autostart = true AUTOSTART=TRUE ";  

}  

// End -->  

</script>  

------------------------------------------------------------  
4.自己制作鼠标  
------------------------------------------------------------
以下代码加入<head>区域  
<style>  
a{ text-decoration:none; cursor: url('link.ani') }  
a:hover{ TEXT-DECORATION:underline overline; cursor: url('link.ani') }  
body { font-family:arial,helvetica,Tahoma; font-size: 9pt; cursor: url('body.cur') }  
TD{ font-size:9pt }  
</style>  
先做两个图标body.cur , link.ani即可

------------------------------------------------------------  
5.让背景图不滚动  
------------------------------------------------------------
IE浏览器支持一个 Body 属性 bgproperties,它可以让背景不滚动:  
〈Body Background="图片文件" bgproperties="fixed"〉  

------------------------------------------------------------  
6.简单制作背景音乐  
------------------------------------------------------------                                                                                      <embed src="music.mid" autostart="true" loop="true" hidden="true"> 对Netscape ,IE 都适用

------------------------------------------------------------  
7.定制浏览器地址栏前的小图标  
------------------------------------------------------------

在的head间加入以下代码:

<link rel="shortcuticon" href="http://…/icon.ico">

其中 icon.ico 为 16x16 的图标文件,颜色不要超过 16 色。

<link rel="Bookmark" href="http://…/icon.ico"> 在收藏夹中显示出你的图标

------------------------------------------------------------  
8.添加到收藏夹  
------------------------------------------------------------
〈a href="j avascript:window.external.addFavorite("http://链接","说明");"〉添加到收藏夹〈/a〉

------------------------------------------------------------  
9.设为首页
------------------------------------------------------------
〈a href=# onclick=this.style.behavior="url(#default#homepage)";this.setHomePage("http://链接");〉设为首页〈/a〉

------------------------------------------------------------  
10.判断输入内容是否为空
------------------------------------------------------------    
function IsNull(){    
    
var str = document.getElementById('str').value.trim();            //or          document.form1.str.value.trim()
    
if(str.length==0){    
                alert('对不起,文本框不能为空或者为空格
!');//请将“文本框”改成你需要验证的属性名称!    
            }
    
}
   

------------------------------------------------------------  
11
.asp.net 中的时间

------------------------------------------------------------
<span id="webasp_time"></span>
<script>setInterval("webasp_time.innerHTML=new Date().toLocaleString()+' 星期'+'日一二三四五六'.charAt (new Date().getDay());",1000);
</script>

------------------------------------------------------------  
12.强制弹窗
------------------------------------------------------------
<script>
showModelessDialog('news.htm','winname','dialogWidth:500px;dialogHeight:500px;dialogLeft:400px;dialogTop:250px;center:yes;help:yes;resizable:no;status:no')
</script>

------------------------------------------------------------  
13.状态栏文字的显示,这个简单实用!
------------------------------------------------------------  
<BASE onmouseover="window.status='欢迎光临!';return true">
------------------------------------------------------------  
14.防止被人frame
------------------------------------------------------------
<head>中加    <base target="_top" />
<SCRIPT LANGUAGE=JAVASCRIPT>
<!--
if (top.location != self.location)top.location=self.location;
// -->
</SCRIPT>
------------------------------------------------------------  
15.删除加确认
<a href='javascript:if(confirm("确实要删除吗?"))location="del.asp"'>删除</a>
------------------------------------------------------------  
16.点击复制文本的内容
------------------------------------------------------------
<html>
<head>
<script language="javascript" type="text/javascript">
          window.onload=function()
          {
document.getElementById("path").value=" http://www.baidu.com ";
          }
          function copy()
          {
document.getElementById("path").select();
document.execCommand("copy");
              alert("复制成功!");
          }
</script>
</head>
<body>
<input type='text' name="path" onclick="copy()" readonly="true" />
</body>
</html>
------------------------------------------------------------  
17.强制弹窗
------------------------------------------------------------
<script>
showModelessDialog('news.htm','winname','dialogWidth:500px;dialogHeight:500px;dialogLeft:400px;dialogTop:250px;center:yes;help:yes;resizable:no;status:no')
</script>
------------------------------------------------------------  
18.控件的显示与隐藏
------------------------------------------------------------
document.getElementById("myctr").style.display = "";              //显示
document.getElementById("myctr").style.display = "none";      //隐藏
------------------------------------------------------------  
19.禁止页面运行出错信息
------------------------------------------------------------
window.onerror = function()
      {
        return true;
      }

------------------------------------------------------------  
20.显示器的分辨率
------------------------------------------------------------
window.srceen.width 得到屏幕的宽度
window.srceen.height 得到屏幕的高度
------------------------------------------------------------  
21 .定时运行特定代码
------------------------------------------------------------
setTimeout(Code,Timeout);
setInterval(Code,Timeout);
Code是一段字符串,里边是js代码,Timeout是时间间隔,单位是微秒
setTimeout是从现在算起多少微秒后运行该代码(只运行一次)
setInterval是每隔多少微秒运行一次代码

------------------------------------------------------------  
22.本页网址
------------------------------------------------------------
var Url = window.location.href;

------------------------------------------------------------  
23.保护自己的页面不被放在框架中  

------------------------------------------------------------  
<Script LANGUAGE="JavaScript">  
if(self!=TOP){ TOP.location=self.location; }  
</script>
------------------------------------------------------------  
24.取消选取、防止复制
------------------------------------------------------------
<body onselectstart="return false">
------------------------------------------------------------  
25. 不准粘贴
------------------------------------------------------------
onpaste="return false"
------------------------------------------------------------  
26. 防止复制
------------------------------------------------------------
oncopy="return false;" oncut="return false;"

------------------------------------------------------------  
27. IE地址栏前换成 自己 的图标
------------------------------------------------------------
<link rel="Shortcut Icon" href="favicon.ico">
------------------------------------------------------------  
28. 可以在收藏夹中显示出你的图标
------------------------------------------------------------
<link rel="Bookmark" href="favicon.ico">
------------------------------------------------------------  
29. 判断上一个页面的来源
------------------------------------------------------------
document.referrer
------------------------------------------------------------  
30. 关闭输入法
------------------------------------------------------------
<input style="ime-mode:disabled">
------------------------------------------------------------

你可能感兴趣的:(常用js函数收集)