Common JavaScript tips
Event source object
Event. srcelement. tagname
Event. srcelement. Type
.................................
Capture and release
Event. srcelement. setcapture ();
Event. srcelement. releasecapture ();
Event buttons
Event. keycode
Event. shiftkey
Event. altkey
Event. ctrlkey
Event Return Value
Event. returnvalue
Mouse position
Event. x
Event. Y
Form Activity element
Document. activeelement
Bind event
Document. captureevents (event. keydown );
Question form Element
Document. All ("TXT"). Focus ();
Document. All ("TXT"). Select ();
Form commands
Document.exe ccommand
Form cookie
Document. Cookie
Menu events
Document. oncontextmenu
Create Element
Document. createelement ("span ");
Obtain the element with the mouse:
Document. elementfrompoint (event. X, event. Y). tagname = "TD
Document. elementfrompoint (event. X, event. Y). appendchild (MS)
Form Image
Document. Images [Index]
Form event binding
Document. onmousedown = scrollwindow;
Element
Document. Form. elements [Index]
Object binding event
Number of plug-ins
Navigator. plugins
Variable type
Typeof ($ js_libpath) = "undefined"
Drop-down list
Drop-down box. Options [Index]
Drop-down box. Options. Length
Search for objects
Document. getelementsbyname ("R1 ");
Document. getelementbyid (ID );
Timing
Uncode Encoding
Escape (), Unescape
Parent object
OBJ. parentelement (DHTML)
OBJ. parentnode (DOM)
Exchange table rows
Tableid. moverow (2, 1)
Replace CSS
Document.all.css S. href = "a.css ";
Side by side
Display: inline
Hide focus
Hidefocus = true
Line feed Based on Width
Style = "word-break: Break-all"
Auto refresh
<Meta HTTP-EQUIV = "refresh" content = "8; url = http://c98.yeah.net">
Simple email
<A href = "mailto: aaa@bbb.com? Subject = CCC & Body = xxxyyy ">
Quickly go to location
OBJ. scrollintoview (true)
Anchor
<A name = "first">
<A href = "# First"> anchors </a>
Webpage Transfer Parameters
Location. Search ();
Editable
OBJ. contenteditable = true
Run the menu command
Obj.exe ccommand
Double-byte characters
/[^ \ X00-\ xFF]/
Chinese characters
[\ U4e00-\ u9fa5]/
Automatically wrap an English string out of the table width
Word-wrap: Break-word; Word-break: Break-all;
Transparent background
<IFRAME src = "1.htm" width = 300 Height = 180 allowtransparency> </iframe>
Get style content
Obj.style.css text
HTML Tag
Document.doc umentelement. innerhtml
First style label
Document. stylesheets [0]
The first style in the style label
Document. stylesheets [0]. Rules [0]
When you click an empty link, the page is often reset to the top.
<A href = "javascript: function ()"> word </a>
Previous webpage Source
ASP:
Request. servervariables ("http_referer ")
Javascript:
Document. referrer
Release memory
Collectgarbage ();
Disable right-click
Document. oncontextmenu = function () {return false ;}
Save prohibited
<NoScript> <IFRAME src = "*. htm"> </iframe> </NoScript>
Deselected
<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 ()>
Disable pasting
<Input type = text onpaste = "Return false">
Address bar icon
<LINK rel = "shortcut icon" href = "favicon. ICO">
It is recommended that the favicon. ICO name remain the same as 16 colors for 16*16. Put it in the root directory of the virtual directory.
Add to favorites bar icon
<LINK rel = "bookmark" href = "favicon. ICO">
View Source Code
Disable Input Method
<Input style = "ime-mode: Disabled">
Automatically select all
<Input type = text name = text1 value = "123" onfocus = "This. Select ()">
Enter to move the cursor to the next input box
<Input onkeydown = "If (event. keycode = 13) event. keycode = 9">
Default Value of the text box
<Input type = text value = "123" onfocus = "alert (this. defaultvalue)">
Title line feed
OBJ. Title = "123
Sdfs"
Obtain the microseconds represented by the time
VaR n1 = new date ("2004-10-10". Replace (/-/g, "\/"). gettime ()
Close Window?
Win. Closed
Checkbox flat
<Input type = checkbox style = "position: absolute; clip: rect (5px 15px 15px 5px)">
Obtain selected content
Document. selection. createRange (). Duplicate (). Text
Automatic completion
<Input type = text AutoComplete = on> enable this function
<Input type = text AutoComplete = off> disable this function.
Window Maximization
<Body onload = "window. resizeTo (window. Screen. Width-4, window. Screen. Height-50); window. moveTo (-4,-4)">
No close button IE
Window. Open ("aa.htm", "meizz", "fullscreen = 7 ");
Unified encoding/Decoding
Alert (decodeuricomponent (encodeuricomponent ("http: // hello. com? As = hehe ")))
Encodeuricomponent for ":", "/", ";" and "? "Also Encoding
Table row indication
// Various dimensions
S + = "\ r \ n visible area width of the webpage:" + document. Body. clientwidth;
S + = "\ r \ n visible area of the webpage:" + document. Body. clientheight;
S + = "\ r \ n visible area of the webpage Height:" + document. Body. offsetweight + "(including the width of the edge )";
S + = "\ r \ n visible area height of the webpage:" + document. Body. offsetheight + "(including the width of the edge )";
S + = "\ r \ n webpage body full text width:" + document. Body. scrollwidth;
S + = "\ r \ n webpage text Height:" + document. Body. scrollheight;
S + = "\ r \ n webpage volume Height:" + document. Body. scrolltop;
S + = "\ r \ n left when the webpage is rolled:" + document. Body. scrollleft;
S + = "\ r \ n webpage body part:" + window. screentop;
S + = "\ r \ n left part of the webpage body:" + window. screenleft;
S + = "\ r \ n screen resolution Height:" + window. Screen. height;
S + = "\ r \ n screen resolution width:" + window. Screen. width;
S + = "\ r \ n available workspace Height:" + window. Screen. availheight;
S + = "\ r \ n available workspace width of the screen:" + window. Screen. availwidth;
// No Cache
<Meta HTTP-EQUIV = "Pragma" content = "no-Cache">
<Meta HTTP-EQUIV = "cache-control" content = "no-cache, must-revalidate">
<Meta HTTP-EQUIV = "expires" content = "0">
// Regular Expression matching
Regular Expression matching Chinese characters: [\ u4e00-\ u9fa5]
Match double-byte characters (including Chinese characters): [^ \ x00-\ xFF]
Regular Expression for matching empty rows: \ n [\ s |] * \ r
Regular Expressions matching HTML tags:/<(. *)>. * <\/\ 1> | <(. *) \/>/
Regular Expressions matching spaces at the beginning and end: (^ \ s *) | (\ s * $) (TRIM functions like VBScript)
The regular expression matching the email address: \ W + ([-+.] \ W +) * @ \ W + ([-.] \ W + )*\. \ W + ([-.] \ W + )*
The regular expression matching the URL: http: // ([\ W-] + \.) + [\ W-] + (/[\ W -./? % & =] *)?
The following is an example:
Use regular expressions to restrict text box input in a webpage form:
// remove image toolbar
[/html]
or
[/html]
// obtain the absolute position of the Control (1)
all options prompt: you can modify some Code and then press run]
// obtain the absolute position of the control (2)
Orect = obj. getboundingclientrect ();
Orect. Left
Orect.
// Print the page
<P style = "Page-break-after: Always"> page1 </P>
<P style = "Page-break-after: Always"> page2 </P>
// Set the print
<Br/>
<Object ID = "Factory" style = "display: none" viewastext <br/>
Classid = "CLSID: 1663ed61-23eb-11d2-b92f-008048fdd814" <br/>
Codebase = "http://www.meadroid.com/scriptx/ScriptX.cab#Version=5,60,0,360" <br/>
> </Object> <br/>
<Input type = button value = page setting onclick = "factory. Printing. pagesetup ()"> <br/>
<Input type = button value = print preview onclick = "factory. Printing. Preview ()"> <br/>
<Br/>
<Script language = JavaScript> <br/>
Function window. onload () <br/>
{<Br/>
// -- Advanced features <br/>
Factory. Printing. setmarginmeasure (2) // measure margins in inches <br/>
Factory. Printing. setpagerange (false, 1, 3) // need pages from 1 to 3 <br/>
Factory. Printing. Printer = "HP Deskjet 870c" <br/>
Factory. Printing. Copies = 2 <br/>
Factory. Printing. collate = true <br/>
Factory. Printing. papersize = "A4" <br/>
Factory. Printing. papersource = "manual feed" <br/>
// -- Basic Features <br/>
Factory. Printing. header = "center left display & center B display & B center right display page number, page & P/total & P" <br/>
Factory. Printing. footer = "(custom footer)" <br/>
Factory. Printing. Portrait = false <br/>
Factory. Printing. leftmargin = 0.75 <br/>
Factory. Printing. topmargin = 1.5 <br/>
Factory. Printing. rightmargin = 0.75 <br/>
Factory. Printing. bottommargin = 1.5 <br/>
} <Br/>
Function print (FRAME) {<br/>
Factory. Printing. Print (true, frame) // print with prompt <br/>
} <Br/>
</SCRIPT> <br/>
<Input type = button value = "print this page" onclick = "factory. Printing. Print (false)"> <br/>
<Input type = button value = "page setting" onclick = "factory. Printing. pagesetup ()"> <br/>
<Input type = button value = "print preview" onclick = "factory. Printing. Preview ()"> <br/>
<A href = "http://www.meadroid.com/scriptx/docs/printdoc.htm? Static "target = _ blank> User Manual. For more information, click here </a> <br/>
Tip for Selecting All: you can modify some code and then press run.]
// Embedded print preview
Webbrowser. execwb () Open
Web. execwb () Close all existing IE Windows and open a new window
Web. execwb () save Web Page
Web. execwb (6, 1) Printing
Web. execwb (7,1) print and preview
Web. execwb (8, 1) Printing page settings
Web. execwb () view page properties
Web. execwb () seems to be undo, to be confirmed
Select all web. execwb ()
Web. execwb (22, 1) Refresh
Web. execwb () Close form no prompt
<Style media = print>
. Noprint {display: None ;}<! -- Hide non-print items when printing with this style -->
. Pagenext {page-break-after: Always ;}<! -- Control pagination -->
</Style>
<Object ID = "webbrowser" width = 0 Height = 0 classid = "CLSID: 8856f961-340a-11d0-a96b-00c04fd705a2">
</Object>
<Center class = "noprint">
<Input type = button value = print onclick = Document. All. webbrowser. execwb (6, 1)>
<Input type = button value = print onclick = Document. All. webbrowser. execwb (6, 6)>
<Input type = button value = page setting onclick = Document. All. webbrowser. execwb (8, 1)>
</P>
<P> <input type = button value = print preview onclick = Document. All. webbrowser. execwb (7,1)>
</Center>
// Remove the header and footer when printing
<Br/>
<Script language = "JavaScript"> <br/>
VaR hkey_root, hkey_path, hkey_key; <br/>
Hkey_root = "HKEY_CURRENT_USER"; <br/>
Hkey_path = "\ Software \ Microsoft \ Internet Explorer \ pagesetup \"; <br/>
// Set the page header and footer to be blank. <br/>
Function pagesetup_null () <br/>
{<Br/>
Try <br/>
{<Br/>
VaR wsh = new activexobject ("wscript. Shell"); <br/>
Hkey_key = "Header"; <br/>
Wsh. regwrite (hkey_root + hkey_path + hkey_key, ""); <br/>
Hkey_key = "footer"; <br/>
Wsh. regwrite (hkey_root + hkey_path + hkey_key, ""); <br/>
} <Br/>
Catch (e) {}< br/>
} <Br/>
// Set the page header and footer to the default value. <br/>
Function pagesetup_default () <br/>
{<Br/>
Try <br/>
{<Br/>
VaR wsh = new activexobject ("wscript. Shell"); <br/>
Hkey_key = "Header"; <br/>
Wsh. regwrite (hkey_root + hkey_path + hkey_key, "& W & B page number, & P/& P"); <br/>
Hkey_key = "footer"; <br/>
Wsh. regwrite (hkey_root + hkey_path + hkey_key, "& U & B & D"); <br/>
} <Br/>
Catch (e) {}< br/>
} <Br/>
</SCRIPT> <br/>
<Input type = "button" value = "Clear page number" onclick = pagesetup_null ()> <br/>
<Input type = "button" value = "Restore page number" onclick = pagesetup_default ()> <br/>
Tip for Selecting All: you can modify some code and then press run.]
// Prompt box with no Mode
Function modelessalert (MSG)
{
Window. showmodelessdialog ("javascript: Alert (\" "+ escape (MSG) +" \ "); window. close (); "," "," status: No; resizable: No; help: No; dialogheight: Height: 30px; dialogheight: 40px ;");
}
// Download an object
Function downurl (strremoteurl, strlocalurl)
{
Try
{
VaR XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
XMLHTTP. Open ("get", strremoteurl, false );
XMLHTTP. Send ();
VaR adodbstream = new activexobject ("ADODB. Stream ");
Adodbstream. type = 1; // 1 = adtypebinary
Adodbstream. open ();
Adodbstream. Write (XMLHTTP. responsebody );
Adodbstream. savetofile (strlocalurl, 2 );
Adodbstream. Close ();
Adodbstream = NULL;
XMLHTTP = NULL;
}
Catch (E)
{
Window. Confirm ("An error occurred while downloading the URL! ");
}
// Window. Confirm ("download completed .");
}
// Check whether the connection is valid
Function getxml (URL)
{
VaR XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
XMLHTTP. Open ("get", URL, false );
Try
{
XMLHTTP. Send ();
}
Catch (e ){}
Finally
{
VaR result = XMLHTTP. responsetext;
If (result)
{
If (XMLHTTP. Status = 200)
{
Return (true );
}
Else
{
Return (false );
}
}
Else
{
Return (false );
}
}
}
// Check whether the webpage exists
Function checkurl (URL)
{
VaR XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
XMLHTTP. Open ("get", URL, false );
Try
{
XMLHTTP. Send ();
VaR result = XMLHTTP. status;
}
Catch (e) {return (false );}
If (result = 200)
{
Return true;
}
XMLHTTP = NULL;
Return false;
}
// Disable FSO
1. log out of the component
Regsvr32/u scrrun. dll
2. Modify progid
Hkey_classes_root \ Scripting. FileSystemObject
Scripting. FileSystemObject
3. for users who use objects, modify hkey_classes_root \ Scripting.