⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usejavascript.txt

📁 常用的javascript例子。都是非常经典的。
💻 TXT
📖 第 1 页 / 共 3 页
字号:
网页常用技巧 

1.禁止右键 
<body oncontextmenu=return(false)> 

2.禁止选择 
<body onselectstart="return false"> 

3.禁止粘贴 
<body onpaste="return false"> 

4.禁止直接访问 [必须框架内才行] 
<script> 
if (top == self)top.location.href = "page.htm"; 
</script> 

5.禁止frame引用 
<script> 
if (top != self)top.location.href = "page.htm"; 
</script> 

6.禁止功能键Shift,Alt,Ctrl 
<script>function key(){ 
if(event.shiftKey) alert("Shift不允许使用!");} document.onkeydown=key; </script> 


数据取得类 

1.取得分辨率 
<script>document.write("宽为"+screen.Width+"高为"+screen.Height)</script> 


2.取得地址栏 
<script>document.write(self.location)</script> 

3.取得地址栏?后参数 
<SCRIPT>var add = top.location; 
add = add.toString(); 
document.write (add.substring(add.indexOf("?")+1,add.length)); 
</SCRIPT> 


常用特效类 

1.主页遥控器 
文件一.(t1.html) 
<SCRIPT language="JavaScript"> 
window.open("t2.html","_blank","width=200","height=200","scroll=no"); 
</SCRIPT> 
文件二.(t2.html) 
<SCRIPT language="JavaScript"> 
function op(add){if (window.opener){window.opener.document.location = add;}} 
</SCRIPT> 
<a href=# onClick="op(’link1.html’)">地址1 </a><br><a href=# 
onClick="op(’link2.html’)">地址2 </a><br><a href=# 
onClick="op(’http://music.jx165.com’)">地址3 </a> 


2.只弹一次的窗口 
<script> 
function get_cookie(Name) { 
var search = Name + "=" 
var returnvalue = ""; 
if (document.cookie.length > 0) {offset = document.cookie.indexOf(search) 
if (offset != -1) {offset += search.length;end = document.cookie.indexOf(";", 
offset); 
if (end == -1)end = 
document.cookie.length;returnvalue=unescape(document.cookie.substring(offset, 
end))} 
} 
return returnvalue; 
} 
function jx165ad(){ 
if (get_cookie(’jx165ad’)==’’){ 
document.cookie="jx165ad=yes" 
window.open("ad.html","_blank","width=200","height=200","scroll=no"); 
} 
else {} 
} 
</script> 
<body> 
<script> 
jx165ad(); 
</script> 

1. oncontextmenu="window.event.returnvalue=false" 将彻底屏蔽鼠标右键
<table border oncontextmenu=return(false)><td>no</table> 可用于Table

2. <body onselect start="return false"> 取消选取、防止复制

3. onpaste="return false" 不准粘贴

4. oncopy="return false;" oncut="return false;" 防止复制

5. <link rel="Shortcut Icon" href="favicon.ico"> IE地址栏前换成自己的图标

6. <link rel="Bookmark" href="favicon.ico"> 可以在收藏夹中显示出你的图标

7. <无效 style="ime-mode:-Disabled"> 关闭输入法

8. 永远都会带着框架
<script language="javascript"><!--
if (window == top)top.本地.href = "frames.htm"; //frames.htm为框架网页
// --></script>

9. 防止被人frame
<script LANGUAGE=javascript><!--
if (top.本地 != self.本地)top.本地=self.本地;
// --></script>

10. 网页将不能被另存为
<noscript><隐藏框架 src=*.html></隐藏框架></noscript>

11. <无效 无效=button value=查看网页源代码
onclick="window.本地 = `view-source:`+ http://www.51js.com/`";>

12.删除时确认
<a href=`java脚本:if(confirm("确实要删除吗?"))本地="boos.asp?&areyou=删除&page=1"`>删除</a>

13. 取得控件的绝对位置
//java脚本
<脚本 language="java脚本">
function getIE(E){
var t=e.offsetTop;
var l=e.offsetLeft;
while(e=e.offsetParent){
t+=e.offsetTop;
l+=e.offsetLeft;
}
alert("top="+t+"/nleft="+l);
}
</脚本>//VB脚本
<脚本 language="VB脚本"><!--
function getIE()
dim t,l,a,b
set a=文档.all.img1
t=文档.all.img1.offsetTop
l=文档.all.img1.offsetLeft
while a.tagName<>"BODY"
set a = a.offsetParent
t=t+a.offsetTop
l=l+a.offsetLeft
wend
msgbox "top="&t&chr(13)&"left="&l,64,"得到控件的位置"
end function
--></脚本>

14. 光标是停在文本框文字的最后
<脚本 language="java脚本">
function cc()
{
var e = event.srcElement;
var r =e.createTextRange();
r.moveStart(`character`,e.value.length);
r.collapse(true);
r.select();
}
</脚本>
<无效 无效=text name=text1 value="123" onfocus="cc()">

15. 判断上一页的来源
java脚本:
文档.referrer

16. 最小化、最大化、关闭窗口
<object id=hh1 classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">
<param name="Command" value="Minimize"></object>
<object id=hh2 classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">
<param name="Command" value="Maximize"></object>
<OBJECT id=hh3 classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11">
<PARAM NAME="Command" value="Close"></OBJECT><无效 无效=button value=最小化 onclick=hh1.Click()>
<无效 无效=button value=最大化 onclick=hh2.Click()>
<无效 无效=button value=关闭 onclick=hh3.Click()>
本例适用于IE

17.屏蔽功能键Shift,Alt,Ctrl
<脚本>
function look(){
if(event.shiftKey)
alert("禁止按Shift键!"); //可以换成ALT CTRL
}
文档.onkeydown=look;
</脚本>

18. 网页不会被缓存
<META HTTP-EQUIV="pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-r执行idate">
<META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">
或者<META HTTP-EQUIV="expires" CONTENT="0">

19.怎样让表单没有凹凸感?
<无效 无效=text style="border:1 solid #000000">
<无效 无效=text style="border-left:none; border-right:none; border-top:none; border-bottom:1 solid #000000"></textarea>

20.<div><span>&<layer>的区别?
<div>(division)用来定义大段的页面元素,会产生转行
<span>用来定义同一行内的元素,跟<div>的唯一区别是不产生转行
<layer>是ns的标记,ie不支持,相当于<div>

21.让弹出窗口总是在最上面:
<body onblur="this.focus();">

22.不要滚动条?
让竖条没有:
<body style=`overflow:-Scroll;overflow-y:hidden`>
</body>
让横条没有:
<body style=`overflow:-Scroll;overflow-x:hidden`>
</body>
两个都去掉?更简单了
<body Scroll="no">
</body>

23.怎样去掉图片链接点击后,图片周围的虚线?
<a href="#" onFocus="this.blur()"><img src="logo.jpg" border=0></a>24.电子邮件处理提交表单
<form name="form1" method="post" action="mailto:****@***.com" enc无效="text/plain">
<无效 无效=submit>
</form>

25.在打开的子窗口刷新父窗口的代码里如何写?
window.opener.本地.reload()

26.如何设定打开页面的大小
<body onload="top.resizeTo(300,200);">
打开页面的位置<body onload="top.moveBy(300,200);">

27.在页面中如何加入不是满铺的背景图片,拉动页面时背景图不动
<style>
body
{background-image:url(logo.gif); background-repeat:no-repeat;background-position:center;background-attachment: fixed}
</style>

28. 检查一段字符串是否全由数字组成
<脚本 language="java脚本"><!--
function checkNum(str){return str.match(//D/)==null}
alert(checkNum("1232142141"))
alert(checkNum("123214214a1"))
// --></脚本>

29. 获得一个窗口的大小
文档.body.clientWidth; 文档.body.clientHeight

30. 怎么判断是否是字符
if (/[^/x00-/xff]/g.test(s)) alert("含有汉字");
else alert("全是字符");

31.TEXTAREA自适应文字行数的多少
<textarea rows=1 name=s1 cols=27 onpropertychange="this.style.posHeight=this.ScrollHeight">
</textarea>

32. 日期减去天数等于第二个日期
<脚本 language=java脚本>
function cc(dd,dadd)
{
//可以加上错误处理
var a = new Date(dd)
a = a.valueOf()
a = a - dadd * 24 * 60 * 60 * 1000
a = new Date(A)
alert(a.getFullYear() + "年" + (a.getMonth() + 1) + "月" + a.getDate() + "日")
}
cc("12/23/2002",2)
</脚本>

33. 选择了哪一个Radio
<HTML><脚本 language="vb脚本">
function checkme()
for each ob in radio1
if ob.checked then window.alert ob.value
next
end function
</脚本><BODY>
<无效 name="radio1" 无效="radio" value="style" checked>style
<无效 name="radio1" 无效="radio" value="barcode">Barcode
<无效 无效="button" value="check" onclick="checkme()">
</BODY></HTML>

34.脚本永不出错
<脚本 LANGUAGE="java脚本">
<!-- Hide
function killErrors() {
return true;
}
window.onerror = killErrors;
// -->
</脚本>

35.ENTER键可以让光标移到下一个输入框
<无效 onkeydown="if(event.keyCode==13)event.keyCode=9">

36. 检测某个网站的链接速度:
把如下代码加入<body>区域中:
<脚本 language=java脚本>
tim=1
setInterval("tim++",100)
b=1var autourl=new Array()
autourl[1]="http://www.njcatv.net/";
autourl[2]="javacool.3322.net"
autourl[3]="http://www.sina.com.cn/";
autourl[4]="http://www.nuaa.edu.cn/";
autourl[5]="http://www.cctv.com/";
function butt(){
文档.write("<form name=autof>")
for(var i=1;i<autourl.length;i++)
文档.write("<无效 无效=text name=txt"+i+" size=10 value=测试中……> =》<无效 无效=textname=url"+i+" size=40> =》<无效 无效=button value=GOonclick=window.open(this.form.url"+i+".value)><br>")
文档.write("<无效 无效=submit value=刷新></form>")
}
butt()
function auto(url){
文档.forms[0]["url"+b].value=url
if(tim>200)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -