---
title: "40种网页常用小技巧(JavaScript)"
author: "AvengerBevis"
date: "2007-04-10T10:17:14Z"
tags: []
categories: ["Web前端"]
slug: "40-javascript"
draft: false
lastmod: "2007-04-10T10:17:14Z"
id: 69525
url: "https://avengerbevis.iteye.com/blog/69525"
---

虽然比较简单  但在需要的时候  找不到 还真的很麻烦  今儿给大家列这  经常搞这些东西的  留一份以备不时之需 也很不错

 

001. 40种网页常用小技巧(JavaScript)
002. 1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键
003. ＜table border oncontextmenu=return(false)＞＜td＞no＜/table＞ 可用于Table
004. 2. ＜body onselectstart="return false"＞ 取消选取、防止复制
005. 3. onpaste="return false" 不准粘贴
006. 4. oncopy="return false;" oncut="return false;" 防止复制
007. 5. ＜link rel="Shortcut Icon" href="favicon.ico"＞ IE地址栏前换成自己的图标
008. 6. ＜link rel="Bookmark" href="favicon.ico"＞ 可以在收藏夹中显示出你的图标
009. 7. ＜input style="ime-mode:disabled"＞ 关闭输入法
010. 8. 永远都会带着框架
011. ＜script language="JavaScript"＞＜!--
012. if (window == top)top.location.href = "frames.htm"; //frames.htm为框架网页
013. // --＞＜/script＞
014. 9. 防止被人frame
015. ＜SCRIPT LANGUAGE=JAVASCRIPT＞＜!--
016. if (top.location != self.location)top.location= self.location;
017. // --＞＜/SCRIPT＞
018. 10. 网页将不能被另存为
019. ＜noscript＞＜iframe src=\*.html＞＜/iframe＞＜/noscript＞
020. 11. ＜input type=button value=查看网页源代码
021. onclick="window.location = "view-source:"+ " http://www.pconline.com.cn""＞
022. 12.删除时确认
023. ＜a href="javascript:if(confirm("确实要删除吗?"))location="boos.asp?&areyou=删除&page=1""＞删除＜/a＞
024. 13. 取得控件的绝对位置
025. //Javascript
026. ＜script language="Javascript"＞
027. function getIE(e){
028. var t=e.offsetTop;
029. var l=e.offsetLeft;
030. while(e=e.offsetParent){
031. t+=e.offsetTop;
032. l+=e.offsetLeft;
033. }
034. alert("top="+t+"/nleft="+l);
035. }
036. ＜/script＞
037. //VBScript
038. ＜script language="VBScript"＞＜!--
039. function getIE()
040. dim t,l,a,b
041. set a=document.all.img1
042. t=document.all.img1.offsetTop
043. l=document.all.img1.offsetLeft
044. while a.tagName＜＞"BODY"
045. set a = a.offsetParent
046. t=t+a.offsetTop
047. l=l+a.offsetLeft
048. wend
049. msgbox "top="&t&chr(13)&"left="&l,64,"得到控件的位置"
050. end function
051. --＞＜/script＞
052. 14. 光标是停在文本框文字的最后
053. ＜script language="javascript"＞
054. function cc()
055. {
056. var e = event.srcElement;
057. var r =e.createTextRange();
058. r.moveStart("character", e.value.length);
059. r.collapse(true);
060. r.select();
061. }
062. ＜/script＞
063. ＜input type=text name=text1 value="123" onfocus="cc()"＞
064. 15. 判断上一页的来源
065. javascript:
066. document.referrer
067. 16. 最小化、最大化、关闭窗口
068. ＜object id=hh1 classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11"＞
069. ＜param name="Command" value="Minimize"＞＜/object＞
070. ＜object id=hh2 classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11"＞
071. ＜param name="Command" value="Maximize"＞＜/object＞
072. ＜OBJECT id=hh3 classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11"＞
073. ＜PARAM NAME="Command" VALUE="Close"＞＜/OBJECT＞
074. ＜input type=button value=最小化 onclick=hh1.Click()＞
075. ＜input type=button value=最大化 onclick=hh2.Click()＞
076. ＜input type=button value=关闭 onclick=hh3.Click()＞
077. 本例适用于IE
078. 17.屏蔽功能键Shift,Alt,Ctrl
079. ＜script＞
080. function look(){
081. if(event.shiftKey)
082. alert("禁止按Shift键!"); //可以换成ALT　CTRL
083. }
084. document.onkeydown=look;
085. ＜/script＞
086. 18. 网页不会被缓存
087. ＜META HTTP-EQUIV="pragma" CONTENT="no-cache"＞
088. ＜META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate"＞
089. ＜META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT"＞
090. 或者＜META HTTP-EQUIV="expires" CONTENT="0"＞
091. 19.怎样让表单没有凹凸感？
092. ＜input type=text style="border:1 solid #000000"＞
093. 或
094. ＜input type=text style="border-left:none; border-right:none; border-top:none; border-bottom:
095. 1 solid #000000"＞＜/textarea＞
096. 20.＜div＞＜span＞&＜layer＞的区别？
097. ＜div＞(division)用来定义大段的页面元素，会产生转行
098. ＜span＞用来定义同一行内的元素，跟＜div＞的唯一区别是不产生转行
099. ＜layer＞是ns的标记，ie不支持，相当于＜div＞
100. 21.让弹出窗口总是在最上面:
101. ＜body onblur="this.focus();"＞
102. 22.不要滚动条?
103. 让竖条没有:
104. ＜body style="overflow:scroll;overflow-y:hidden"＞
105. ＜/body＞
106. 让横条没有:
107. ＜body style="overflow:scroll;overflow-x:hidden"＞
108. ＜/body＞
109. 两个都去掉？更简单了
110. ＜body scroll="no"＞
111. ＜/body＞
112. 23.怎样去掉图片链接点击后，图片周围的虚线？
113. ＜a href="#" onFocus="this.blur()"＞＜img src="logo.jpg" border=0＞＜/a＞
114. 24.电子邮件处理提交表单
115. ＜form name="form1" method="post" action="mailto:\*\*\*\*@\*\*\*.com" enctype="text/plain"＞
116. ＜input type=submit＞
117. ＜/form＞
118. 25.在打开的子窗口刷新父窗口的代码里如何写？
119. window.opener.location.reload()
120. 26.如何设定打开页面的大小
121. ＜body onload="top.resizeTo(300,200);"＞
122. 打开页面的位置＜body onload="top.moveBy(300,200);"＞
123. 27.在页面中如何加入不是满铺的背景图片,拉动页面时背景图不动
124. ＜STYLE＞
125. body
126. {background-image:url(logo.gif); background-repeat:no-repeat;
127. background-position:center;background-attachment: fixed}
128. ＜/STYLE＞
129. 28. 检查一段字符串是否全由数字组成
130. ＜script language="Javascript"＞＜!--
131. function checkNum(str){return str.match(//D/)==null}
132. alert(checkNum("1232142141"))
133. alert(checkNum("123214214a1"))
134. // --＞＜/script＞
135. 29. 获得一个窗口的大小
136. document.body.clientWidth; document.body.clientHeight
137. 30. 怎么判断是否是字符
138. if (/\[^/x00-/xff]/g.test(s)) alert("含有汉字");
139. else alert("全是字符");
140. 31.TEXTAREA自适应文字行数的多少
141. ＜textarea rows=1 name=s1 cols=27 onpropertychange=" this.style.posHeight=this.scrollHeight"＞
142. ＜/textarea＞
143. 32. 日期减去天数等于第二个日期
144. ＜script language=Javascript＞
145. function cc(dd,dadd)
146. {
147. //可以加上错误处理
148. var a = new Date(dd)
149. a = a.valueOf()
150. a = a - dadd * 24 * 60 * 60 * 1000
151. a = new Date(a)
152. alert(a.getFullYear() + "年" + (a.getMonth() + 1) + "月" + a.getDate() + "日")
153. }
154. cc("12/23/2002",2)
155. ＜/script＞
156. 33. 选择了哪一个Radio
157. ＜HTML＞＜script language="vbscript"＞
158. function checkme()
159. for each ob in radio1
160. if ob.checked then window.alert ob.value
161. next
162. end function
163. ＜/script＞＜BODY＞
164. ＜INPUT name="radio1" type="radio" value="style" checked＞Style
165. ＜INPUT name="radio1" type="radio" value="barcode"＞Barcode
166. ＜INPUT type="button" value="check" onclick="checkme()"＞
167. ＜/BODY＞＜/HTML＞
168. 34.脚本永不出错
169. ＜SCRIPT LANGUAGE="JavaScript"＞
170. ＜!-- Hide
171. function killErrors() {
172. return true;
173. }
174. window.onerror = killErrors;
175. // --＞
176. ＜/SCRIPT＞
177. 35.ENTER键可以让光标移到下一个输入框
178. ＜input onkeydown="if(event.keyCode==13)event.keyCode=9"＞
179. 36. 检测某个网站的链接速度：
180. 把如下代码加入＜body＞区域中:
181. ＜script language=Javascript＞
182. tim=1
183. setInterval("tim++",100)
184. b=1
185. var autourl=new Array()
186. autourl\[1]="www.njcatv.net"
187. autourl\[2]="javacool.3322.net"
188. autourl\[3]=" www.sina.com.cn"
189. autourl\[4]="www.nuaa.edu.cn"
190. autourl\[5]="www.cctv.com"
191. function butt(){
192. document.write("＜form name=autof＞")
193. for(var i=1;i＜autourl.length;i++)
194. document.write("＜input type=text name=txt"+i+" size=10 value=测试中……＞ =》＜input type=text
195. name=url"+i+" size=40＞ =》＜input type=button value=GO
196. onclick=window.open(this.form.url"+i+".value)＞＜br＞")
197. document.write("＜input type=submit value=刷新＞＜/form＞")
198. }
199. butt()
200. function auto(url){
201. document.forms\[0]\["url"+b].value=url
202. if(tim＞200)
203. {document.forms\[0]\["txt"+b].value="链接超时"}
204. else
205. {document.forms\[0]\["txt"+b].value="时间"+tim/10+"秒"}
206. b++
207. }
208. function run(){for(var i=1;i＜autourl.length;i++)document.write("＜img src=http://"+autourl+"/"+Math.random()+" width=1 height=1
209. onerror=auto("http://"+autourl+"")＞")}
210. run()＜/script＞
211. 37. 各种样式的光标
212. auto ：标准光标
213. default ：标准箭头
214. hand ：手形光标
215. wait ：等待光标
216. text ：I形光标
217. vertical-text ：水平I形光标
218. no-drop ：不可拖动光标
219. not-allowed ：无效光标
220. help ：?帮助光标
221. all-scroll ：三角方向标
222. move ：移动标
223. crosshair ：十字标
224. e-resize
225. n-resize
226. nw-resize
227. w-resize
228. s-resize
229. se-resize
230. sw-resize
231. 38.页面进入和退出的特效
232. 进入页面＜meta http-equiv="Page-Enter" content="revealTrans(duration=x, transition=y)"＞
233. 推出页面＜meta http-equiv="Page-Exit" content="revealTrans(duration=x, transition=y)"＞
234. 这个是页面被载入和调出时的一些特效。duration表示特效的持续时间，以秒为单位。transition表示使用哪种特效，取值为1-23:
235. 0 矩形缩小
236. 1 矩形扩大
237. 2 圆形缩小
238. 3 圆形扩大
239. 4 下到上刷新
240. 5 上到下刷新
241. 6 左到右刷新
242. 7 右到左刷新
243. 8 竖百叶窗
244. 9 横百叶窗
245. 10 错位横百叶窗
246. 11 错位竖百叶窗
247. 12 点扩散
248. 13 左右到中间刷新
249. 14 中间到左右刷新
250. 15 中间到上下
251. 16 上下到中间
252. 17 右下到左上
253. 18 右上到左下
254. 19 左上到右下
255. 20 左下到右上
256. 21 横条
257. 22 竖条
258. 23 以上22种随机选择一种
259. 39.在规定时间内跳转
260. ＜META http-equiv=V="REFRESH" content="5;URL= http://www.51js.com"＞
261. 40.网页是否被检索
262. ＜meta name="ROBOTS" content="属性值"＞
263. 其中属性值有以下一些:
264. 属性值为"all": 文件将被检索，且页上链接可被查询；
265. 属性值为"none": 文件不被检索，而且不查询页上的链接；
266. 属性值为"index": 文件将被检索；
267. 属性值为"follow": 查询页上的链接；
268. 属性值为"noindex": 文件不检索，但可被查询链接；
269. 属性值为"nofollow": 文件不被检索，但可查询页上的链接。
270. Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=245829