Javascript 经典实用代码

复制代码
1 1 . oncontextmenu = " window.event.returnValue=false " 将彻底屏蔽鼠标右键
2
3   < table border oncontextmenu = return ( false ) >< td > no < / table> 可用于Table
4
5 2 . < body onselectstart = " return false " > 取消选取、防止复制
6
7 3 . onpaste = " return false " 不准粘贴
8
9 4 . oncopy = " return false; " oncut = " return false; " 防止复制
10
11 5 . < link rel = " Shortcut Icon " href = " favicon.ico " > IE地址栏前换成自己的图标
12
13 6 . < link rel = " Bookmark " href = " favicon.ico " > 可以在收藏夹中显示出你的图标
14
15 7 . < input style = " ime-mode:disabled " > 关闭输入法
16
17 8 . 永远都会带着框架
18
19 < script language = " JavaScript " >
24
25 9 . 防止被人frame
26
27 < SCRIPT LANGUAGE = JAVASCRIPT >
32
33 10 . 网页将不能被另存为
34
35 < noscript >< iframe src =* .html >< / iframe>< / noscript >
36
37 11 . < input type = button value = 查看网页源代码
38
39 onclick = " window.location = " view - source: " + " http: // www.pconline.com.cn"">
40
41 12 .删除时确认
42
43 < a href = " javascript:if(confirm( " 确实要删除吗 ? " ))location= " boos.asp ?& areyou = 删除 & page = 1 "" > 删除 < / a>
44
45 13 . 取得控件的绝对位置
46
47 // Javascript
48
49 < script language = " Javascript " >
50
51 function getIE(e){
52
53 var t = e.offsetTop;
54
55 var l = e.offsetLeft;
56
57 while (e = e.offsetParent){
58
59 t += e.offsetTop;
60
61 l += e.offsetLeft;
62
63 }
64
65 alert( " top= " + t + " /nleft= " + l);
66
67 }
68
69 < / script>
70
71 // VBScript
72
73 < script language = " VBScript " >< / script>
100
101 14 . 光标是停在文本框文字的最后
102
103 < script language = " javascript " >
104
105 function cc()
106
107 {
108
109 var e = event.srcElement;
110
111 var r = e.createTextRange();
112
113 r.moveStart( " character " ,e.value.length);
114
115 r.collapse( true );
116
117 r.select();
118
119 }
120
121 < / script>
122
123 < input type = text name = text1 value = " 123 " onfocus = " cc() " >
124
125 15 . 判断上一页的来源
126
127 javascript:
128
129 document.referrer
130
131 16 . 最小化、最大化、关闭窗口
132
133 < object id = hh1 classid = " clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11 " >
134
135 < param name = " Command " value = " Minimize " >< / object>
136
137 < object id = hh2 classid = " clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11 " >
138
139 < param name = " Command " value = " Maximize " >< / object>
140
141 < OBJECT id = hh3 classid = " clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11 " >
142
143 < PARAM NAME = " Command " VALUE = " Close " >< / OBJECT>
144
145 < input type = button value = 最小化 onclick = hh1.Click() >
146
147 < input type = button value = 最大化 onclick = hh2.Click() >
148
149 < input type = button value = 关闭 onclick = hh3.Click() >
150
151 本例适用于IE
152
153 17 .屏蔽功能键Shift,Alt,Ctrl
154
155 < script >
156
157 function look(){
158
159 if (event.shiftKey)
160
161 alert( " 禁止按Shift键! " ); // 可以换成ALT CTRL
162
163 }
164
165 document.onkeydown = look;
166
167 < / script>
168
169 18 . 网页不会被缓存
170
171 < META HTTP - EQUIV = " pragma " CONTENT = " no-cache " >
172
173 < META HTTP - EQUIV = " Cache-Control " CONTENT = " no-cache, must-revalidate " >
174
175 < META HTTP - EQUIV = " expires " CONTENT = " Wed, 26 Feb 1997 08:21:57 GMT " >
176
177 或者 < META HTTP - EQUIV = " expires " CONTENT = " 0 " >
178
179 19 .怎样让表单没有凹凸感?
180
181 < input type = text style = " border:1 solid #000000 " >
182
183
184
185 < input type = text style = " border-left:none; border-right:none; border-top:none; border-bottom:
186
187 1 solid #000000 " >< / textarea>
188
189 20 . < div >< span >&< layer > 的区别?
190
191 < div > (division)用来定义大段的页面元素,会产生转行
192
193 < span > 用来定义同一行内的元素,跟 < div > 的唯一区别是不产生转行
194
195 < layer > 是ns的标记,ie不支持,相当于 < div >
196
197 21 .让弹出窗口总是在最上面:
198
199 < body onblur = " this.focus(); " >
200
201 22 .不要滚动条 ?
202
203 让竖条没有:
204
205 < body style = " overflow:scroll;overflow-y:hidden " >
206
207 < / body>
208
209 让横条没有:
210
211 < body style = " overflow:scroll;overflow-x:hidden " >
212
213 < / body>
214
215 两个都去掉?更简单了
216
217 < body scroll = " no " >
218
219 < / body>
220
221 23 .怎样去掉图片链接点击后,图片周围的虚线?
222
223 < a href = " # " onFocus = " this.blur() " >< img src = " logo.jpg " border = 0 >< / a>
224
225 24 .电子邮件处理提交表单
226
227 < form name = " form1 " method = " post " action = " mailto:****@***.com " enctype = " text/plain " >
228
229 < input type = submit >
230
231 < / form>
232
233 25 .在打开的子窗口刷新父窗口的代码里如何写?
234
235 window.opener.location.reload()
236
237 26 .如何设定打开页面的大小
238
239 < body onload = " top.resizeTo(300,200); " >
240
241 打开页面的位置 < body onload = " top.moveBy(300,200); " >
242
243 27 .在页面中如何加入不是满铺的背景图片,拉动页面时背景图不动
244
245 < STYLE >
246
247 body
248
249 { background - image:url(logo.gif); background - repeat:no - repeat;
250
251 background - position:center;background - attachment: fixed }
252
253 < / STYLE>
254
255 28 . 检查一段字符串是否全由数字组成
256
257 < script language = " Javascript " >
266
267 29 . 获得一个窗口的大小
268
269 document.body.clientWidth; document.body.clientHeight
270
271 30 . 怎么判断是否是字符
272
273 if ( / [^ / x00 - / xff] / g.test(s)) alert( " 含有汉字 " );
274
275 else alert( " 全是字符 " );
276
277 31 .TEXTAREA自适应文字行数的多少
278
279 < textarea rows = 1 name = s1 cols = 27 onpropertychange = " this.style.posHeight=this.scrollHeight " >
280
281 < / textarea>
282
283 32 . 日期减去天数等于第二个日期
284
285 < script language = Javascript >
286
287 function cc(dd,dadd)
288
289 {
290
291 // 可以加上错误处理
292
293 var a = new Date(dd)
294
295 a = a.valueOf()
296
297 a = a - dadd * 24 * 60 * 60 * 1000
298
299 a = new Date(a)
300
301 alert(a.getFullYear() + " " + (a.getMonth() + 1 ) + " " + a.getDate() + " " )
302
303 }
304
305 cc( " 12/23/2002 " , 2 )
306
307 < / script>
308
309 33 . 选择了哪一个Radio
310
311 < HTML >< script language = " vbscript " >
312
313 function checkme()
314
315 for each ob in radio1
316
317 if ob.checked then window.alert ob.value
318
319 next
320
321 end function
322
323 < / script>
324
325 < INPUT name = " radio1 " type = " radio " value = " style " checked > Style
326
327 < INPUT name = " radio1 " type = " radio " value = " barcode " > Barcode
328
329 < INPUT type = " button " value = " check " onclick = " checkme() " >
330
331 < / BODY>< / HTML >
332
333 34 .脚本永不出错
334
335 < SCRIPT LANGUAGE = " JavaScript " >
336
337
348
349 < / SCRIPT>
350
351 35 .ENTER键可以让光标移到下一个输入框
352
353 < input onkeydown = " if(event.keyCode==13)event.keyCode=9 " >
354
355 36 . 检测某个网站的链接速度:
356
357 把如下代码加入 < body > 区域中:
358
359 < script language = Javascript >
360
361 tim = 1
362
363 setInterval( " tim++ " , 100 )
364
365 b = 1
366
367 var autourl = new Array()
368
369 autourl[ 1 ] = " www.njcatv.net "
370
371 autourl[ 2 ] = " javacool.3322.net "
372
373 autourl[ 3 ] = " www.sina.com.cn "
374
375 autourl[ 4 ] = " www.nuaa.edu.cn "
376
377 autourl[ 5 ] = " www.cctv.com "
378
379 function butt(){
380
381 document.write( "
" )
382
383 for ( var i = 1 ;i < autourl.length;i ++ )
384
385 document.write( " " + i + " size=10 value=测试中……> =》 386
387 name=url " + i + " size=40> =》 388
389 οnclick=window.open(this.form.url " + i + " .value)>
" )
390
391 document.write( " " )
392
393 }
394
395 butt()
396
397 function auto(url){
398
399 document.forms[ 0 ][ " url " + b].value = url
400
401 if (tim > 200 )
402
403 { document.forms[ 0 ][ " txt " + b].value = " 链接超时 " }
404
405 else
406
407 { document.forms[ 0 ][ " txt " + b].value = " 时间 " + tim / 10+"秒" }
408
409 b ++
410
411 }
412
413 function run(){ for ( var i = 1 ;i < autourl.length;i ++ )document.write( " " + autourl + " / " + Math.random() + " width=1 height=1
414
415 οnerrοr=auto( " http: // "+autourl+"")>") }
416
417 run() < / script>
418
419 37 . 各种样式的光标
420
421 auto :标准光标
422
423 default :标准箭头
424
425 hand :手形光标
426
427 wait :等待光标
428
429 text :I形光标
430
431 vertical - text :水平I形光标
432
433 no - drop :不可拖动光标
434
435 not - allowed :无效光标
436
437 help : ? 帮助光标
438
439 all - scroll :三角方向标
440
441 move :移动标
442
443 crosshair :十字标
444
445 e - resize
446
447 n - resize
448
449 nw - resize
450
451 w - resize
452
453 s - resize
454
455 se - resize
456
457 sw - resize
458
459 38 .页面进入和退出的特效
460
461 进入页面 < meta http - equiv = " Page-Enter " content = " revealTrans(duration=x, transition=y) " >
462
463 推出页面 < meta http - equiv = " Page-Exit " content = " revealTrans(duration=x, transition=y) " >
464
465 这个是页面被载入和调出时的一些特效。duration表示特效的持续时间,以秒为单位。transition表示使用哪种特效,取值为1 - 23 :
466
467    0 矩形缩小
468
469    1 矩形扩大
470
471    2 圆形缩小
472
473    3 圆形扩大
474
475    4 下到上刷新
476
477    5 上到下刷新
478
479    6 左到右刷新
480
481    7 右到左刷新
482
483    8 竖百叶窗
484
485    9 横百叶窗
486
487    10 错位横百叶窗
488
489    11 错位竖百叶窗
490
491    12 点扩散
492
493    13 左右到中间刷新
494
495    14 中间到左右刷新
496
497    15 中间到上下
498
499    16 上下到中间
500
501    17 右下到左上
502
503    18 右上到左下
504
505    19 左上到右下
506
507    20 左下到右上
508
509    21 横条
510
511    22 竖条
512
513    23 以上22种随机选择一种
514
515 39 .在规定时间内跳转
516
517 < META http - equiv = V = " REFRESH " content = " 5;URL=http://www.51js.com " >
518
519 40 .网页是否被检索
520
521 < meta name = " ROBOTS " content = " 属性值 " >
522
523   其中属性值有以下一些:
524
525   属性值为 " all " : 文件将被检索,且页上链接可被查询;
526
527   属性值为 " none " : 文件不被检索,而且不查询页上的链接;
528
529   属性值为 " index " : 文件将被检索;
530
531   属性值为 " follow " : 查询页上的链接;
532
533   属性值为 " noindex " : 文件不检索,但可被查询链接;
534
535   属性值为 " nofollow " : 文件不被检索,但可查询页上的链接。
536
复制代码

你可能感兴趣的:(Javascript 经典实用代码)