Home >Web Front-end >JS Tutorial >A complete collection of Javascript tips_JavaScript
Event source object
event.srcElement.tagName
event.srcElement.type
Capture release
event.srcElement.setCapture();
event.srcElement.releaseCapture();
Event key
event.keyCode
event.shiftKey
event.altKey
event.ctrlKey
Event return value
event.returnValue
Mouse position
event. x
event.y
Form active element
document.activeElement
Bind event
document.captureEvents(Event.KEYDOWN);
Access form element
document. all("txt").focus();
document.all("txt").select();
Form command
document.execCommand
Form COOKIE
document. cookie
Menu event
document.oncontextmenu
Create element
document.createElement("SPAN");
Get element based on mouse:
document.elementFromPoint(event.x,event. y).tagName=="TD
document.elementFromPoint(event.x,event.y).appendChild(ms)
Form pictures
document.images[index]
Form event binding Define
document.onmousedown=scrollwindow;
Element
document.Form.elements[index]
Object binding event
document.all.xxx.detachEvent('onclick',a) ;
Number of plug-ins
navigator.plugins
Get variable type
typeof($js_libpath) == "undefined"
Drop-down box
Drop-down box.options[index]
Drop-down Box.options.length
Find object
document.getElementsByName("r1");
document.getElementById(id);
Timing
timer=setInterval('scrollwindow()',delay );
clearInterval(timer);
UNCODE encoding
escape(),unescape
Parent object
obj.parentElement(dhtml)
obj.parentNode(dom)
Exchange Table rows
TableID.moveRow(2,1)
Replace CSS
document.all.csss.href = "a.css";
Display side by side
display:inline
Hide focus
hidefocus=true
Wrap line according to width
style="word-break:break-all"
Auto refresh
Simple email
Quick Go to location
obj.scrollIntoView(true)
Anchors
anchors
Web page passing parameters
location.search();
Editable
obj.contenteditable=true
Execute menu command
obj.execCommand
Double-byte characters
/[ ^x00-xff]/
Chinese characters
/[u4e00-u9fa5]/
Let the English string automatically wrap when it exceeds the width of the table
word-wrap: break-word; word-break: break-all ;
Transparent background
Get style content
obj.style.cssText
HTML Tag
document.documentElement.innerHTML
The first style tag
document.styleSheets[0]
The first style in the style tag
document.styleSheets[0].rules[0 ]
Prevent the page from being reset to the top of the page when clicking on an empty link.
word
Previous web page source
asp:
request.servervariables("HTTP_REFERER")
javascript:
document.referrer
Release memory
CollectGarbage();
Disable right click
document.oncontextmenu = function() { return false;}
Disable saving
Disable selection
//Various sizes
s = "rnThe width of the visible area of the web page:" document.body.clientWidth;
s = "rnThe height of the visible area of the web page: " document.body.offsetWeight
" (including the width of the edge)"; s = "rnWeb page Visible area height: " document.body.offsetHeight
" (including the width of the edge)"; s = "rn Full text width of the web page: " document.body .scrollWidth;
s = "rnThe full text height of the web page:" document.body.scrollHeight;
s = "rnThe height of the web page being scrolled:" document .body.scrollTop;
s = "rnThe left side of the web page being scrolled:" document.body.scrollLeft;
s = "rnThe main text of the web page:" window.screenTop;
s = "rnThe left side of the text of the web page:" window.screenLeft;
s = "rnThe high screen resolution:" window. screen.height;
s = "rnScreen resolution width:" window.screen.width;
s = "rnScreen available work area height:" window.screen.availHeight;
s = "rnScreen available workspace width:" window.screen.availWidth;
"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">
"window.external.ImportExportFavorites(true,'http://localhost');">
"window.external.ImportExportFavorites(false,'http://localhost');">
"window.external.ShowBrowserUI('OrganizeFavorites', null)">
"window.external.ShowBrowserUI('LanguageDialog', null)">
"window.external.AddFavorite(' http://www.google.com/', 'google')">
"window. external.addChannel('http://www.google.com/')">
"window .external.showBrowserUI('PrivacySettings',null)">
//No caching
"pragma" CONTENT="no-cache">
"Cache -Control" CONTENT="no-cache, must-revalidate">
"expires" CONTENT=" 0">
//Regular matching
Regular expression matching Chinese characters: [u4e00-u9fa5]
Matching double-byte characters (including Chinese characters): [^x00-xff]
Regular expression matching blank lines: n[ s| ]*r
Regular expression matching HTML tags: /<(.*)>.*1>|<(.*) />/
Matching leading and trailing spaces Regular expression: (^s*)|(s*$) (trim function like vbscript)
Regular expression matching email addresses: w ([- .]w )*@w ([-.] w )*.w ([-.]w )*
Regular expression matching URL: http://([w-] .) [w-] (/[w- ./? %&=]*)?
The following is an example:
Use regular expressions to limit the input content of the text box in the web form:
Use regular expressions to limit only Chinese input: onkeyup="value=value.replace(/[^u4E00-u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace (/[^u4E00-u9FA5]/g,''))"
1. Use regular expressions to limit the input of only full-width characters: onkeyup="value=value.replace(/[^ uFF00-uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^uFF00-uFFFF]/g,' '))"
2. Use regular expressions to limit input to numbers: onkeyup="value=value.replace(/[^d]/g,'') "onbeforepaste ="clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]/g,''))"
3. Use regular expressions to limit Only numbers and English can be entered: onkeyup="value=value.replace(/[W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData ('text').replace(/[^d]/g,''))"
//Eliminate image toolbar
"mypicture.jpg" HEIGHT=" 100px" WIDTH="100px" GALLERYIMG="false">
or
"imagetoolbar" content="no">
//Close without prompt
function Close()
{
var ua=navigator.userAgent
var ie=navigator.appName=="Microsoft Internet Explorer"?true:false
if(ie)
{
var IEversion=parseFloat(ua.substring(ua.indexOf("MSIE ") 5,ua.indexOf(";",ua.indexOf("MSIE "))))
if(IEversion< 5.5)
{
var str = '';
document.body.insertAdjacentHTML("beforeEnd", str);
document.all.noTipClose.Click();
}
else
{
window.opener =null;
window.close();
}
}
else
{
window.close()
}
}
//取得控件得绝对位置(1)
//获得控件的绝对位置(2)
oRect = obj.getBoundingClientRect();
oRect.left
oRect.
//最小化,最大化,关闭
//光标停在文字最后
"123" onfocus="cc()">
//页面进入和退出的特效
进入页面"Page-Enter" content="revealTrans(duration=x, transition=y)">
推出页面"Page-Exit" content="revealTrans(duration=x, transition=y)">
这个是页面被载入和调出时的一些特效。duration表示特效的持续时间,以秒为单位。transition表示使
用哪种特效,取值为1-23:
0 矩形缩小
1 矩形扩大
2 圆形缩小
3 圆形扩大
4 下到上刷新
5 上到下刷新
6 左到右刷新
7 右到左刷新
8 竖百叶窗
9 横百叶窗
10 错位横百叶窗
11 错位竖百叶窗
12 点扩散
13 左右到中间刷新
14 中间到左右刷新
15 中间到上下
16 上下到中间
17 右下到左上
18 右上到左下
19 左上到右下
20 左下到右上
21 横条
22 竖条
23
//网页是否被检索
"ROBOTS" content="Attribute value">
The attribute values are as follows:
The attribute value is "all": The files will be retrieved, and the links on the page can be queried;
The attribute value is "none": The files will not be retrieved, and the links on the page will not be queried;
The attribute value is "index": The file will be retrieved;
The attribute value is "follow": The link on the query page;
The attribute value is "noindex": The file is not retrieved, but can be queried for links;
The attribute value is "nofollow":
//Print page break
"page-break-after:always" >page1
"page-break-after:always">page2
//Set up printing
"factory.printing.PageSetup()">
"factory.printing.Preview()">
"打印本页" onclick="factory.printing.Print(false)">
"页面设置" onclick="factory.printing.PageSetup()">
"打印预览" onclick="factory.printing.Preview()">
"http://www.meadroid.com/scriptx/docs/printdoc.htm?static" target=_blank>具体使用手册,更多信息,点这里
//自带的打印预览
WebBrowser.ExecWB(1,1) Open
Web.ExecWB(2,1) Close all current IE windows and open a new window
Web.ExecWB(4,1) Save Web page
Web.ExecWB(6,1) Print
Web.ExecWB(7,1) Print preview
Web.ExecWB(8,1) Print page settings
Web.ExecWB(10,1 ) View page properties
Web.ExecWB(15,1) It seems to be revoked and needs to be confirmed
Web.ExecWB(17,1) Select all
Web.ExecWB(22,1) Refresh
Web .ExecWB(45,1) Close the form without prompting
//Remove the header and footer when printing
function
modelessAlert(Msg){ window.showModelessDialog("javascript:alert(""
escape(Msg)
"");window.close();" ,"","status:no;resizable:no;help:no;dialogHeight:height:30px;dialogHeight:40px;"); }
//Block key
//To move the layer, drag
) onmousemove=MouseMove() onmouseup=MouseUp()>meizz "myp" //文档状态改变
1.this
src=
"logo.gif" ondrag="doDrag ();"
onmouseover=
"this.style.cursor='hand'"
style=
"position:absolute;left=100;top=100;"
onmousedown= "doMouseDown();"> "#" onclick="return false">wlecome< ;/h1>
<script> <br><span style="COLOR: #0000ff">var</span> doc=window.frames[<span style="COLOR: #ff00ff">"f"</span>].document; <br><span style="COLOR: #0000ff">function</span> s(){ <br> <span style="COLOR: #0000ff">if</span> (doc.readyState==<span style="COLOR: #ff00ff">"complete"</span>){ <br> document.all.f.style.height=doc.body.scrollHeight <br> document.all.f.style.width=doc.body.scrollWidth <br> } <br>} <br>doc.onreadystatechange=s <br></script>
//刷新后不变的文本框
//访问剪贴板
(1)拖拽访问
event.dataTransfer.setData("URL", oImage.src);
sImageURL = event.dataTransfer.getData("URL")
(2)普通访问
window.clipboardData.setData("Text",oSource.innerText);
window.clipboardData.getData("Text");
//操作COOKIE
function SetCookie(sName, sValue)
{
document.cookie = sName "=" escape(sValue) "; ";
}
function GetCookie(sName)
{
var aCookie = document.cookie.split("; ");
for (var i=0; i < aCookie.length; i )
{
var aCrumb = aCookie[i].split("=");
if (sName == aCrumb[0])
return unescape(aCrumb[1]);
}
}
function DelCookie(sName)
{
document.cookie = sName "=" escape(sValue) "; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}
//setTimeout增加参数
<script> <br><span style="COLOR: #0000ff">var</span> _st = window.setTimeout; <br>window.setTimeout = <span style="COLOR: #0000ff">function</span>(fRef, mDelay) { <br> <span style="COLOR: #0000ff">if</span>(typeof fRef == '<span style="COLOR: #0000ff">function</span>'){ <br> <span style="COLOR: #0000ff">var</span> argu = <span style="COLOR: #ff0000">Array</span>.prototype.slice.call(arguments,2); <br> <span style="COLOR: #0000ff">var</span> f = (<span style="COLOR: #0000ff">function</span>(){ fRef.apply(<span style="COLOR: #0000ff">null</span>, argu); }); <br> <span style="COLOR: #0000ff">return</span> _st(f, mDelay); <br> } <br> <span style="COLOR: #0000ff">return</span> _st(fRef,mDelay); <br>} <br><span style="COLOR: #0000ff">function</span> test(x){ <br> alert(x); <br>} <br>window.setTimeout(test,1000,'fason'); <br></script>