javascript技巧大全套

  1 事件源对象 
  2 event.srcElement.tagName 
  3 event.srcElement.type 
  4 捕获释放 
  5 event.srcElement.setCapture();  
  6 event.srcElement.releaseCapture();  
  7 事件按键 
  8 event.keyCode 
  9 event.shiftKey 
 10 event.altKey 
 11 event.ctrlKey 
 12 事件返回值 
 13 event.returnValue 
 14 鼠标位置 
 15 event.x 
 16 event.y 
 17 窗体活动元素 
 18 document.activeElement 
 19 绑定事件 
 20 document.captureEvents(Event.KEYDOWN); 
 21 访问窗体元素 
 22 document.all("txt").focus(); 
 23 document.all("txt").select(); 
 24 窗体命令 
 25 document.execCommand 
 26 窗体COOKIE 
 27 document.cookie 
 28 菜单事件 
 29 document.oncontextmenu 
 30 创建元素 
 31 document.createElement("SPAN");  
 32 根据鼠标获得元素: 
 33 document.elementFromPoint(event.x,event.y).tagName=="TD 
 34 document.elementFromPoint(event.x,event.y).appendChild(ms)  
 35 窗体图片 
 36 document.images[索引] 
 37 窗体事件绑定 
 38 document.onmousedown=scrollwindow; 
 39 元素 
 40 document.窗体.elements[索引] 
 41 对象绑定事件 
 42 document.all.xxx.detachEvent('onclick',a); 
 43 插件数目 
 44 navigator.plugins 
 45 取变量类型 
 46 typeof($js_libpath) == "undefined" 
 47 下拉框 
 48 下拉框.options[索引] 
 49 下拉框.options.length 
 50 查找对象 
 51 document.getElementsByName("r1"); 
 52 document.getElementById(id); 
 53 定时 
 54 timer=setInterval('scrollwindow()',delay); 
 55 clearInterval(timer); 
 56 UNCODE编码 
 57 escape() ,unescape 
 58 父对象 
 59 obj.parentElement(dhtml) 
 60 obj.parentNode(dom) 
 61 交换表的行 
 62 TableID.moveRow(2,1) 
 63 替换CSS 
 64 document.all.csss.href = "a.css"; 
 65 并排显示 
 66 display:inline 
 67 隐藏焦点 
 68 hidefocus=true 
 69 根据宽度换行 
 70 style="word-break:break-all" 
 71 自动刷新 
 72 <meta HTTP-EQUIV="refresh" CONTENT="8;URL=http://c98.yeah.net"> 
 73 简单邮件 
 74 <a  href="mailto:aaa@bbb.com?subject=ccc&body=xxxyyy">  
 75 快速转到位置 
 76 obj.scrollIntoView(true) 
 77  78 <a name="first"> 
 79 <a href="#first">anchors</a> 
 80 网页传递参数 
 81 location.search(); 
 82 可编辑 
 83 obj.contenteditable=true 
 84 执行菜单命令 
 85 obj.execCommand 
 86 双字节字符 
 87 /[^\x00-\xff]/ 
 88 汉字 
 89 /[\u4e00-\u9fa5]/ 
 90 让英文字符串超出表格宽度自动换行 
 91 word-wrap: break-word; word-break: break-all; 
 92 透明背景 
 93 <IFRAME src="1.htm" width=300 height=180 allowtransparency></iframe> 
 94 获得style内容 
 95 obj.style.cssText 
 96 HTML标签 
 97 document.documentElement.innerHTML 
 98 第一个style标签 
 99 document.styleSheets[0] 
100 style标签里的第一个样式 
101 document.styleSheets[0].rules[0] 
102 防止点击空链接时,页面往往重置到页首端。 
103 <a href="javascript:function()">word</a> 
104 上一网页源 
105 asp: 
106 request.servervariables("HTTP_REFERER") 
107 javascript: 
108 document.referrer 
109 释放内存 
110 CollectGarbage(); 
111 禁止右键 
112 document.oncontextmenu = function() { return false;} 
113 禁止保存 
114 <noscript><iframe src="*.htm"></iframe></noscript> 
115 禁止选取<body oncontextmenu="return false" ondragstart="return false" onselectstart ="return false" onselect="document.selection.empty()" oncopy="document.selection.empty()" onbeforecopy="return false"onmouseup="document.selection.empty()>  
116 禁止粘贴 
117 <input type=text onpaste="return false"> 
118 地址栏图标 
119 <link rel="Shortcut Icon" href="favicon.ico"> 
120 favicon.ico 名字最好不变16*16的16色,放虚拟目录根目录下 
121 收藏栏图标 
122 <link rel="Bookmark" href="favicon.ico"> 
123 查看源码 
124 <input type=button value=查看网页源代码 onclick="window.location = 'view-source:'+ 'http://www.csdn.net/'"> 
125 关闭输入法 
126 <input style="ime-mode:disabled"> 
127 自动全选 
128 <input type=text name=text1 value="123" onfocus="this.select()"> 
129 ENTER键可以让光标移到下一个输入框 
130 <input onkeydown="if(event.keyCode==13)event.keyCode=9"> 
131 文本框的默认值 
132 <input type=text value="123" onfocus="alert(this.defaultValue)"> 
133 title换行 
134 obj.title = "123 sdfs " 
135 获得时间所代表的微秒 
136 var n1 = new Date("2004-10-10".replace(/-/g, "\/")).getTime() 
137 窗口是否关闭 
138 win.closed 
139 checkbox扁平 
140 <input type=checkbox style="position: absolute; clip:rect(5px 15px 15px 5px)"><br> 
141 获取选中内容 
142 document.selection.createRange().duplicate().text 
143 自动完成功能 
144 <input  type=text  autocomplete=on>打开该功能  
145 <input  type=text  autocomplete=off>关闭该功能    
146 窗口最大化 
147 <body onload="window.resizeTo(window.screen.width - 4,window.screen.height-50);window.moveTo(-4,-4)"> 
148 无关闭按钮IE 
149 window.open("aa.htm", "meizz", "fullscreen=7"); 
150 统一编码/解码 
151 alert(decodeURIComponent(encodeURIComponent("http://你好.com?as= hehe"))) 
152 encodeURIComponent对":"、"/"、";" 和 "?"也编码 
153 表格行指示 
154 <tr onmouseover="this.bgColor='#f0f0f0'" onmouseout="this.bgColor='#ffffff'"> 
155 
156 //各种尺寸 
157 
158 s  +=  "\r\n网页可见区域宽:"+  document.body.clientWidth;   
159 s  +=  "\r\n网页可见区域高:"+  document.body.clientHeight;   
160 s  +=  "\r\n网页可见区域高:"+  document.body.offsetWeight  +"  (包括边线的宽)";   
161 s  +=  "\r\n网页可见区域高:"+  document.body.offsetHeight  +"  (包括边线的宽)";   
162 s  +=  "\r\n网页正文全文宽:"+  document.body.scrollWidth;   
163 s  +=  "\r\n网页正文全文高:"+  document.body.scrollHeight;   
164 s  +=  "\r\n网页被卷去的高:"+  document.body.scrollTop;   
165 s  +=  "\r\n网页被卷去的左:"+  document.body.scrollLeft;   
166 s  +=  "\r\n网页正文部分上:"+  window.screenTop;   
167 s  +=  "\r\n网页正文部分左:"+  window.screenLeft;   
168 s  +=  "\r\n屏幕分辨率的高:"+  window.screen.height;   
169 s  +=  "\r\n屏幕分辨率的宽:"+  window.screen.width;   
170 s  +=  "\r\n屏幕可用工作区高度:"+  window.screen.availHeight;   
171 s  +=  "\r\n屏幕可用工作区宽度:"+  window.screen.availWidth;   
172 //过滤数字 
173 
174 <input type=text onkeypress="return event.keyCode>=48&&event.keyCode<=57||(this.value.indexOf('.')<0?event.keyCode==46:false)" onpaste="return !clipboardData.getData('text').match(/\D/)" ondragenter="return false"> 
175  
176 
177 //特殊用途 
178 
179 <input type=button value=导入收藏夹 onclick="window.external.ImportExportFavorites(true,'http://localhost');"> 
180 <input type=button value=导出收藏夹 onclick="window.external.ImportExportFavorites(false,'http://localhost');"> 
181 <input type=button value=整理收藏夹 onclick="window.external.ShowBrowserUI('OrganizeFavorites', null)"> 
182 <input type=button value=语言设置   onclick="window.external.ShowBrowserUI('LanguageDialog', null)"> 
183 <input type=button value=加入收藏夹 onclick="window.external.AddFavorite('http://www.google.com/', 'google')"> 
184 <input type=button value=加入到频道 onclick="window.external.addChannel('http://www.google.com/')"> 
185 <input type=button value=加入到频道 onclick="window.external.showBrowserUI('PrivacySettings',null)"> 
186  
187 
188 //不缓存 
189 
190 <META HTTP-EQUIV="pragma" CONTENT="no-cache"> 
191 <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate"> 
192 <META HTTP-EQUIV="expires" CONTENT="0"> 
193  
194 
195  
196 
197 
198 
199 //正则匹配 
200  
201 
202 匹配中文字符的正则表达式: [\u4e00-\u9fa5] 
203 匹配双字节字符(包括汉字在内):[^\x00-\xff] 
204 匹配空行的正则表达式:\n[\s| ]*\r 
205 匹配HTML标记的正则表达式:/<(.*)>.*<\/\1>|<(.*) \/>/  
206 匹配首尾空格的正则表达式:(^\s*)|(\s*$)(像vbscript那样的trim函数) 
207 匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* 
208 匹配网址URL的正则表达式:http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)? 
209 以下是例子: 
210 利用正则表达式限制网页表单里的文本框输入内容: 
211 用正则表达式限制只能输入中文:onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))" 
212 1.用正则表达式限制只能输入全角字符: onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))" 
213 2.用正则表达式限制只能输入数字:onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" 
214 3.用正则表达式限制只能输入数字和英文:onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" 
215 //消除图像工具栏 
216 
217 <IMG SRC="mypicture.jpg" HEIGHT="100px" WIDTH="100px" GALLERYIMG="false">  
218 or 
219 <head> 
220 <meta http-equiv="imagetoolbar" content="no"> 
221 </head> 
222  
223 
224 //无提示关闭 
225 
226 
227 function Close() 
228 { 
229  var ua=navigator.userAgent 
230  var ie=navigator.appName=="Microsoft Internet Explorer"?true:false 
231  if(ie) 
232  { 
233       var IEversion=parseFloat(ua.substring(ua.indexOf("MSIE ")+5,ua.indexOf(";",ua.indexOf("MSIE ")))) 
234   if(IEversion< 5.5) 
235   { 
236    var str  = '<object id=noTipClose classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">' 
237        str += '<param name="Command" value="Close"></object>'; 
238        document.body.insertAdjacentHTML("beforeEnd", str); 
239        document.all.noTipClose.Click(); 
240   } 
241       else 
242   { 
243        window.opener =null; 
244        window.close(); 
245       } 
246    } 
247  else 
248  { 
249   window.close() 
250    } 
251 } 
252 //取得控件得绝对位置(1) 
253 
254 
255 <script language="javascript">  
256 function getoffset(e) 
257 {  
258  var t=e.offsetTop;  
259  var l=e.offsetLeft;  
260  while(e=e.offsetParent) 
261  {  
262   t+=e.offsetTop;  
263   l+=e.offsetLeft;  
264  }  
265  var rec = new Array(1); 
266  rec[0]  = t; 
267  rec[1] = l; 
268  return rec 
269 }  
270 </script> 
271 //获得控件的绝对位置(2) 
272 
273 oRect = obj.getBoundingClientRect(); 
274 oRect.left 
275 oRect. 
276 //最小化,最大化,关闭 
277 
278 <object id=min classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">  
279 <param name="Command" value="Minimize"></object>  
280 <object id=max classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">  
281 <param name="Command" value="Maximize"></object>  
282 <OBJECT id=close classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11">  
283 <PARAM NAME="Command" value="Close"></OBJECT>  
284 <input type=button value=最小化 onclick=min.Click()>  
285 <input type=button value=最大化 onclick=max.Click()>  
286 <input type=button value=关闭 onclick=close.Click()>  
287  
288 
289  
290 
291 
292 //光标停在文字最后 
293 
294  
295 
296 <script language="javascript"> 
297 function cc() 
298 { 
299  var e = event.srcElement; 
300  var r =e.createTextRange(); 
301  r.moveStart('character',e.value.length); 
302  r.collapse(true); 
303  r.select(); 
304 } 
305 </script> 
306 <input type=text name=text1 value="123" onfocus="cc()"> 
307 //页面进入和退出的特效 
308 
309 
310 进入页面<meta http-equiv="Page-Enter" content="revealTrans(duration=x, transition=y)"> 
311 推出页面<meta http-equiv="Page-Exit" content="revealTrans(duration=x, transition=y)">  
312 这个是页面被载入和调出时的一些特效。duration表示特效的持续时间,以秒为单位。transition表示使 
313 用哪种特效,取值为1-23: 
314   0 矩形缩小  
315   1 矩形扩大  
316   2 圆形缩小 
317   3 圆形扩大  
318   4 下到上刷新  
319   5 上到下刷新 
320   6 左到右刷新  
321   7 右到左刷新  
322   8 竖百叶窗 
323   9 横百叶窗  
324   10 错位横百叶窗  
325   11 错位竖百叶窗 
326   12 点扩散  
327   13 左右到中间刷新  
328   14 中间到左右刷新 
329   15 中间到上下 
330   16 上下到中间  
331   17 右下到左上 
332   18 右上到左下  
333   19 左上到右下  
334   20 左下到右上 
335   21 横条  
336   22 竖条  
337   23  
338 //网页是否被检索 
339 
340 <meta name="ROBOTS" content="属性值"> 
341   其中属性值有以下一些: 
342   属性值为"all": 文件将被检索,且页上链接可被查询; 
343   属性值为"none": 文件不被检索,而且不查询页上的链接; 
344   属性值为"index": 文件将被检索; 
345   属性值为"follow": 查询页上的链接; 
346   属性值为"noindex": 文件不检索,但可被查询链接; 
347   属性值为"nofollow":  
348  
349 
350 
351 //打印分页 
352 
353 <p  style="page-break-after:always">page1</p>   
354 <p  style="page-break-after:always">page2</p>   
355  
356 
357 
358 //设置打印 
359 
360 
361 <object id="factory" style="display:none" viewastext 
362   classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814" 
363   codebase="http://www.meadroid.com/scriptx/ScriptX.cab#Version=5,60,0,360" 
364 ></object> 
365 <input type=button value=页面设置 onclick="factory.printing.PageSetup()"> 
366 <input type=button value=打印预览 onclick="factory.printing.Preview()"> 
367   
368 <script language=javascript> 
369 function window.onload() 
370 { 
371    // -- advanced features 
372    factory.printing.SetMarginMeasure(2) // measure margins in inches 
373    factory.printing.SetPageRange(false, 1, 3) // need pages from 1 to 3 
374    factory.printing.printer = "HP DeskJet 870C" 
375    factory.printing.copies = 2 
376    factory.printing.collate = true 
377    factory.printing.paperSize = "A4" 
378    factory.printing.paperSource = "Manual feed" 
379    // -- basic features 
380    factory.printing.header = "居左显示&b居中显示&b居右显示页码,第&p页/共&P页" 
381    factory.printing.footer = "(自定义页脚)" 
382    factory.printing.portrait = false 
383    factory.printing.leftMargin = 0.75 
384    factory.printing.topMargin = 1.5 
385    factory.printing.rightMargin = 0.75 
386    factory.printing.bottomMargin = 1.5 
387 } 
388 function Print(frame) { 
389   factory.printing.Print(true, frame) // print with prompt 
390 } 
391 </script> 
392 <input type=button value="打印本页" onclick="factory.printing.Print(false)"> 
393 <input type=button value="页面设置" onclick="factory.printing.PageSetup()"> 
394 <input type=button value="打印预览" onclick="factory.printing.Preview()"><br> 
395 <a href="http://www.meadroid.com/scriptx/docs/printdoc.htm?static"  target=_blank>具体使用手册,更多信息,点这里</a> 
396   
397 //自带的打印预览 
398 
399 WebBrowser.ExecWB(1,1) 打开  
400 Web.ExecWB(2,1) 关闭现在所有的IE窗口,并打开一个新窗口  
401 Web.ExecWB(4,1) 保存网页  
402 Web.ExecWB(6,1) 打印  
403 Web.ExecWB(7,1) 打印预览  
404 Web.ExecWB(8,1) 打印页面设置  
405 Web.ExecWB(10,1) 查看页面属性  
406 Web.ExecWB(15,1) 好像是撤销,有待确认  
407 Web.ExecWB(17,1) 全选  
408 Web.ExecWB(22,1) 刷新  
409 Web.ExecWB(45,1) 关闭窗体无提示  
410 <style media=print>  
411 .Noprint{display:none;}<!--用本样式在打印时隐藏非打印项目-->  
412 .PageNext{page-break-after: always;}<!--控制分页-->  
413 </style>  
414 <object  id="WebBrowser"  width=0  height=0  classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2">     
415 </object>     
416   
417 <center class="Noprint" > 
418 <input type=button value=打印 onclick=document.all.WebBrowser.ExecWB(6,1)>  
419 <input type=button value=直接打印 onclick=document.all.WebBrowser.ExecWB(6,6)>  
420 <input type=button value=页面设置 onclick=document.all.WebBrowser.ExecWB(8,1)>  
421 </p>  
422 <p> <input type=button value=打印预览 onclick=document.all.WebBrowser.ExecWB(7,1)>  
423 </center> 
424 //去掉打印时的页眉页脚 
425 
426 
427 <script  language="JavaScript">   
428 var HKEY_Root,HKEY_Path,HKEY_Key; 
429 HKEY_Root="HKEY_CURRENT_USER"; 
430 HKEY_Path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\"; 
431 //设置网页打印的页眉页脚为空 
432 function PageSetup_Null() 
433 { 
434  try 
435  { 
436          var Wsh=new ActiveXObject("WScript.Shell"); 
437   HKEY_Key="header"; 
438   Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,""); 
439   HKEY_Key="footer"; 
440   Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,""); 
441  } 
442  catch(e){} 
443 } 
444 //设置网页打印的页眉页脚为默认值 
445 function  PageSetup_Default() 
446 {   
447  try 
448  { 
449   var Wsh=new ActiveXObject("WScript.Shell"); 
450   HKEY_Key="header"; 
451   Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"&w&b页码,&p/&P"); 
452   HKEY_Key="footer"; 
453   Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"&u&b&d"); 
454  } 
455  catch(e){} 
456 } 
457 </script> 
458 <input type="button" value="清空页码" onclick=PageSetup_Null()> 
459 <input type="button" value="恢复页码" onclick=PageSetup_Default()> 
460 //浏览器验证 
461 
462 
463 function checkBrowser() 
464 {  
465    this.ver=navigator.appVersion  
466    this.dom=document.getElementById?1:0  
467    this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom)?1:0;  
468    this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;  
469    this.ie4=(document.all && !this.dom)?1:0;  
470    this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;  
471    this.ns4=(document.layers && !this.dom)?1:0;  
472    this.mac=(this.ver.indexOf('Mac') > -1) ?1:0;  
473    this.ope=(navigator.userAgent.indexOf('Opera')>-1);  
474    this.ie=(this.ie6 || this.ie5 || this.ie4)  
475    this.ns=(this.ns4 || this.ns5)  
476    this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns5 || this.ns4 || this.mac || this.ope)  
477    this.nbw=(!this.bw)  
478    return this; 
479 } 
480 //计算内容宽和高 
481 
482 <SCRIPT  language="javascript">   
483 function  test(obj)   
484 {   
485        var  range  =  obj.createTextRange();   
486        alert("内容区宽度:  "  +  range.boundingWidth     
487                                                  +  "px\r\n内容区高度:  "  +  range.boundingHeight  +  "px");   
488               
489 }   
490 </SCRIPT>   
491 <BODY>   
492 <Textarea id="txt" height="150">sdf</textarea><INPUT  type="button"  value="计算内容宽度"  onClick="test(txt)">   
493 </BODY> 
494 //无模式的提示框 
495 
496 function modelessAlert(Msg) 
497 { 
498    window.showModelessDialog("javascript:alert(\""+escape(Msg)+"\");window.close();","","status:no;resizable:no;help:no;dialogHeight:height:30px;dialogHeight:40px;"); 
499 } 
500  
501 
502  
503 
504  
505 
506 
507 //屏蔽按键 
508  
509 
510 <html> 
511 <head> 
512   <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
513   <noscript><meta http-equiv="refresh" content="0;url=about:noscript"></noscript> 
514   <title>屏蔽鼠标右键、Ctrl+N、Shift+F10、Alt+F4、F11、F5刷新、退格键</title> 
515 </head> 
516 <body> 
517 <script language="Javascript"><!-- 
518   //屏蔽鼠标右键、Ctrl+N、Shift+F10、F11、F5刷新、退格键 
519   //Author: meizz(梅花雨) 2002-6-18 
520 function document.oncontextmenu(){event.returnValue=false;}//屏蔽鼠标右键 
521 function window.onhelp(){return false} //屏蔽F1帮助 
522 function document.onkeydown() 
523 { 
524   if ((window.event.altKey)&& 
525       ((window.event.keyCode==37)||   //屏蔽 Alt+ 方向键 ← 
526        (window.event.keyCode==39)))   //屏蔽 Alt+ 方向键 → 
527   { 
528      alert("不准你使用ALT+方向键前进或后退网页!"); 
529      event.returnValue=false; 
530   } 
531      /* 注:这还不是真正地屏蔽 Alt+ 方向键, 
532      因为 Alt+ 方向键弹出警告框时,按住 Alt 键不放, 
533      用鼠标点掉警告框,这种屏蔽方法就失效了。以后若 
534      有哪位高手有真正屏蔽 Alt 键的方法,请告知。*/ 
535   if ((event.keyCode==8)  ||                 //屏蔽退格删除键 
536       (event.keyCode==116)||                 //屏蔽 F5 刷新键 
537       (event.ctrlKey && event.keyCode==82)){ //Ctrl + R 
538      event.keyCode=0; 
539      event.returnValue=false; 
540      } 
541   if (event.keyCode==122){event.keyCode=0;event.returnValue=false;}  //屏蔽F11 
542   if (event.ctrlKey && event.keyCode==78) event.returnValue=false;   //屏蔽 Ctrl+n 
543   if (event.shiftKey && event.keyCode==121)event.returnValue=false;  //屏蔽 shift+F10 
544   if (window.event.srcElement.tagName == "A" && window.event.shiftKey)  
545       window.event.returnValue = false;             //屏蔽 shift 加鼠标左键新开一网页 
546   if ((window.event.altKey)&&(window.event.keyCode==115))             //屏蔽Alt+F4 
547   { 
548       window.showModelessDialog("about:blank","","dialogWidth:1px;dialogheight:1px"); 
549       return false; 
550   } 
551 } 
552 </script> 
553 屏蔽鼠标右键、Ctrl+N、Shift+F10、Alt+F4、F11、F5刷新、退格键 
554 </body> 
555 </html> 
556 //屏蔽打印 
557 <style> 
558 @media print{ 
559 * {display:none} 
560 } 
561 </style> 
562 
563 //移动的图层,拖动 
564 
565 
566 1.<span style='position:absolute;width:200;height:200;background:red' onmousedown=MouseDown(this) onmousemove=MouseMove() onmouseup=MouseUp()>meizz</span> 
567 <script language=javascript> 
568 var Obj; 
569 function MouseDown(obj) 
570 { 
571   Obj=obj; 
572   Obj.setCapture(); 
573   Obj.l=event.x-Obj.style.pixelLeft; 
574   Obj.t=event.y-Obj.style.pixelTop; 
575 } 
576 function MouseMove() 
577 { 
578   if(Obj!=null) 
579   { 
580     Obj.style.left = event.x-Obj.l; 
581     Obj.style.top = event.y-Obj.t; 
582   } 
583 } 
584 function MouseUp() 
585 { 
586   if(Obj!=null) 
587   { 
588     Obj.releaseCapture(); 
589     Obj=null; 
590   } 
591 } 
592 </script> 
593 2. 
594 <div id="myDiv" src="logo.gif" ondrag="doDrag();" onmouseover="this.style.cursor='hand'" style="position:absolute;left=100;top=100;" onmousedown="doMouseDown();"> 
595 <a href="#" onclick="return false"><h1>wlecome</h1></a> 
596 </div> 
597 <script language="JavaScript" type="text/javascript"> 
598 var orgMouseX; 
599 var orgMouseY; 
600 var orgObjX; 
601 var orgObjY; 
602 function doDrag() 
603 { 
604 var myObject=document.all.myDiv; 
605 
606 var x=event.clientX; 
607 var y=event.clientY; 
608 myObject.style.left=x-(orgMouseX-orgObjX); 
609 myObject.style.top=y-(orgMouseY-orgObjY); 
610   
611 } 
612 function doMouseDown() 
613 { 
614 orgMouseX=event.clientX; 
615 orgMouseY=event.clientY; 
616 orgObjX=parseInt(document.all.myDiv.style.left); 
617 orgObjY=parseInt(document.all.myDiv.style.top); 
618 } 
619 
620 </script> 
621   
622 //文档状态改变 
623 
624 
625 <iframe src="a.html" id="f" name="f" scrolling="no" frameborder=0 marginwidth=0 marginheight=0></iframe> 
626 <script> 
627 var doc=window.frames["f"].document; 
628 function s(){ 
629  if (doc.readyState=="complete"){ 
630   document.all.f.style.height=doc.body.scrollHeight 
631   document.all.f.style.width=doc.body.scrollWidth 
632  } 
633 } 
634 doc.onreadystatechange=s 
635 </script> 
636 
637 //刷新后不变的文本框 
638 
639 <HTML> 
640 <HEAD> 
641 <META NAME="save" CONTENT="history"> 
642 <STYLE> 
643    .sHistory {behavior:url(#default#savehistory);} 
644 </STYLE> 
645 </HEAD> 
646 <BODY> 
647 <INPUT class=sHistory type=text id=oPersistInput> 
648 </BODY> 
649 </HTML> 
650  
651 
652 //访问剪贴板 
653 
654 (1)拖拽访问 
655 event.dataTransfer.setData("URL", oImage.src); 
656 sImageURL = event.dataTransfer.getData("URL") 
657 (2)普通访问 
658 window.clipboardData.setData("Text",oSource.innerText); 
659 window.clipboardData.getData("Text"); 
660  
661 
662 //操作COOKIE 
663 
664 
665 function SetCookie(sName, sValue) 
666 { 
667  document.cookie = sName + "=" + escape(sValue) + "; "; 
668 } 
669 function GetCookie(sName) 
670 { 
671  var aCookie = document.cookie.split("; "); 
672  for (var i=0; i < aCookie.length; i++) 
673  { 
674    
675   var aCrumb = aCookie[i].split("="); 
676   if (sName == aCrumb[0])  
677   return unescape(aCrumb[1]); 
678  } 
679   
680 } 
681 function DelCookie(sName) 
682 { 
683 document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 1999 23:59:59 GMT;"; 
684 } 
685 //setTimeout增加参数 
686 
687 <script> 
688 var _st = window.setTimeout; 
689 window.setTimeout = function(fRef, mDelay) { 
690  if(typeof fRef == 'function'){ 
691   var argu = Array.prototype.slice.call(arguments,2); 
692   var f = (function(){ fRef.apply(null, argu); }); 
693   return _st(f, mDelay); 
694  } 
695  return _st(fRef,mDelay); 
696 } 
697 function test(x){ 
698  alert(x); 
699 } 
700 window.setTimeout(test,1000,'fason'); 
701 </script> 
702 
703 //自定义的apply,call 
704 
705 Function.prototype.apply = function (obj, argu) { 
706  if (obj) obj.constructor.prototype._caller = this;  
707  var argus = new Array(); 
708  for (var i=0;i<argu.length;i++) 
709   argus[i] = "argu[" + i + "]"; 
710  var r; 
711  eval("r = " + (obj ? ("obj._caller(" + argus.join(",") + ");") : ("this(" + argus.join(",") + ");"))); 
712  return r; 
713 }; 
714 Function.prototype.call = function (obj) { 
715  var argu = new Array(); 
716  for (var i=1;i<arguments.length;i++) 
717   argu[i-1] = arguments[i]; 
718  return this.apply(obj, argu); 
719 };        
720 
721 //下载文件 
722 
723 function DownURL(strRemoteURL,strLocalURL) 
724 { 
725  try 
726  { 
727   var xmlHTTP=new ActiveXObject("Microsoft.XMLHTTP"); 
728   xmlHTTP.open("Get",strRemoteURL,false); 
729   xmlHTTP.send(); 
730   var adodbStream=new ActiveXObject("ADODB.Stream"); 
731   adodbStream.Type=1;//1=adTypeBinary 
732   adodbStream.Open(); 
733   adodbStream.write(xmlHTTP.responseBody); 
734   adodbStream.SaveToFile(strLocalURL,2); 
735   adodbStream.Close(); 
736   adodbStream=null; 
737   xmlHTTP=null; 
738    
739  } 
740  catch(e) 
741  { 
742   window.confirm("下载URL出错!"); 
743  } 
744  //window.confirm("下载完成."); 
745 } 
746 
747 //检验连接是否有效 
748 
749 function getXML(URL)  
750 { 
751  var xmlhttp = new ActiveXObject("microsoft.xmlhttp"); 
752  xmlhttp.Open("GET",URL, false);  
753  try 
754  {  
755   xmlhttp.Send(); 
756  } 
757  catch(e){} 
758  finally  
759  { 
760   var result = xmlhttp.responseText; 
761   if(result)  
762   { 
763    if(xmlhttp.Status==200) 
764    { 
765     return(true); 
766    } 
767    else  
768    { 
769     return(false); 
770    } 
771   } 
772   else  
773   { 
774    return(false); 
775   } 
776  } 
777 } 
778 //POST代替FORM 
779 
780 <SCRIPT language="VBScript"> 
781 Function URLEncoding(vstrIn) 
782     strReturn = "" 
783     For i = 1 To Len(vstrIn) 
784         ThisChr = Mid(vStrIn,i,1) 
785         If Abs(Asc(ThisChr)) < &HFF Then 
786             strReturn = strReturn & ThisChr 
787         Else 
788             innerCode = Asc(ThisChr) 
789             If innerCode < 0 Then 
790                 innerCode = innerCode + &H10000 
791             End If 
792             Hight8 = (innerCode  And &HFF00)\ &HFF 
793             Low8 = innerCode And &HFF 
794             strReturn = strReturn & "%" & Hex(Hight8) &  "%" & Hex(Low8) 
795         End If 
796     Next 
797     URLEncoding = strReturn 
798 End Function 
799 Function bytes2BSTR(vIn) 
800     strReturn = "" 
801     For i = 1 To LenB(vIn) 
802         ThisCharCode = AscB(MidB(vIn,i,1)) 
803         If ThisCharCode < &H80 Then 
804             strReturn = strReturn & Chr(ThisCharCode) 
805         Else 
806             NextCharCode = AscB(MidB(vIn,i+1,1)) 
807             strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode)) 
808             i = i + 1 
809         End If 
810     Next 
811     bytes2BSTR = strReturn 
812 End Function 
813 dim strA,oReq 
814 strA = URLEncoding("submit1=Submit&text1=中文") 
815 set oReq = CreateObject("MSXML2.XMLHTTP") 
816 oReq.open "POST","http://ServerName/VDir/TstResult.asp",false 
817 oReq.setRequestHeader "Content-Length",Len(strA) 
818 oReq.setRequestHeader "CONTENT-TYPE","application/x-www-form-urlencoded" 
819 oReq.send strA 
820 msgbox bytes2BSTR(oReq.responseBody) 
821 </SCRIPT> 
822 //readyState是xmlhttp返回数据的进度,0=载入中,1=未初始化,2=已载入,3=运行中,4=完成
823 
824  
825 
826  
827 
828 //组件是否安装 
829 isComponentInstalled("{6B053A4B-A7EC-4D3D-4567-B8FF8A1A5739}", "componentID")) 
830 
831 //检查网页是否存在 
832 
833 
834 function CheckURL(URL) 
835 { 
836   var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
837   xmlhttp.Open("GET",URL, false); 
838   try 
839   {  
840     xmlhttp.Send();  
841     var result = xmlhttp.status; 
842   } 
843   catch(e) {return(false); } 
844   if(result==200) 
845   {  
846     return true; 
847   } 
848   xmlhttp = null; 
849   return false; 
850 } 
851 //连接数据库 
852 
853 
854 <script language="javascript"> 
855   //用 JavaScript 写服务器端连接数据库的代码示例 
856   var conn = new ActiveXObject("ADODB.Connection"); 
857   conn.Open("Provider=SQLOLEDB.1; Data Source=localhost; User ID=sa; " 
858     +"Password=; Initial Catalog=pubs"); 
859   var rs = new ActiveXObject("ADODB.Recordset"); 
860   var sql="select * from authors"; 
861   rs.open(sql, conn); 
862  shtml = "<table width='100%' border=1>"; 
863  shtml +="<tr bgcolor='#f4f4f4'><td>au_id</td><td>au_lname</td><td>au_fname</td><td>phone</td><td>address</td><td> city</td><td>state</td><td>zip</td></tr>"; 
864   while(!rs.EOF) 
865   { 
866  shtml += "<tr><td>" + rs("au_id") + "</td><td>" + rs("au_lname") + "</td><td>" + rs("au_fname") + "</td><td>" + rs("phone") + "</td><td>" + rs("address") + "</td><td>" + rs("city") + "</td><td>" + rs("state") + "</td><td>" + rs("zip") + "</td></tr>"; 
867  rs.moveNext; 
868   } 
869   shtml += "</table>"; 
870   document.write(shtml); 
871   rs.close();  
872   rs = null;  
873   conn.close();  
874   conn = null; 
875 </script> 
876 //使用数据岛 
877 
878 
879 <html> 
880 <body> 
881 srno:<input type=text datasrc=#xmldate DataFLD=srno size="76"><BR> 
882 times:<input type=text datasrc=#xmldate DataFLD=times size="76"><BR> 
883 <input id="first" TYPE=button value="<< 第一条记录" onclick="xmldate.recordset.moveFirst()"> 
884 <input id="prev" TYPE=button value="<上一条记录" onclick="xmldate.recordset.movePrevious()">   
885 <input id="next" TYPE=button value="下一条记录>" onclick="xmldate.recordset.moveNext()">   
886 <input id="last" TYPE=button value="最后一条记录>>" onclick="xmldate.recordset.moveLast()">    
887 <input id="Add" TYPE=button value="添加新记录" onclick="xmldate.recordset.addNew()">   
888 
889 <XML ID="xmldate"> 
890 <infolist> 
891 <info ><srno>20041025-01</srno><times>null</times></info> 
892 <info ><srno>20041101-09</srno><times>2004年10月1日2点22分0秒</times></info> 
893 </infolist> 
894 </XML> 
895 </body> 
896 </html> 
897 //获得参数 
898 
899 <body> 
900 <a href="javascript:location.href=location.href + '?a=1&b=2'">search</a> 
901 <script language="JavaScript"> 
902 <!-- 
903 var a = location.search.substr(1); 
904 if(a.length>0) 
905 { 
906  var re = /([^&]*?)\=([^&]*)/g 
907  var s = a.match(re); 
908  for(var i= 0;i<s.length;i++) 
909  { 
910   alert(s[i]); 
911   alert(s[i].split("=")[1]); 
912  } 
913 } 
914 //--> 
915 </script> 
916 </body> 
917 //可编辑SELECT 
918 
919 <input type=text name=re_name style="width:100px;height:21px;font-size:10pt;"><span style="width:18px;border:0px solid red;"><select name="r00" style="margin-left:-100px;width:118px; background-color:#FFEEEE;" onChange="document.all.re_name.value=this.value;"> 
920                 <option value="1">11111111<option> 
921                 <option value="2">222222</option> 
922                 <option value="3">333333</option> 
923               </select> 
924               </span>

 

posted @ 2012-12-28 09:52  Jackin  阅读(1680)  评论(0编辑  收藏  举报