Some of the most common JavaScript tips

Source: Internet
Author: User
Tags empty expression header html tags key tagname window root directory
Javascript| Tips

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 activity elements


Document.activeelement

Binding events


Document.captureevents (Event.keydown);

accessing form elements


document.all ("TXT"). focus ();
document.all ("TXT"). Select ();

Form command


Document.execcommand

form cookies


Document.cookie

Menu Events


Document.oncontextmenu



Creating elements


Document.createelement ("SPAN");

To get elements from the mouse:


Document.elementfrompoint (EVENT.X,EVENT.Y). tagname== "TD
Document.elementfrompoint (EVENT.X,EVENT.Y). appendchild (MS)

Form picture


document.images[Index]

Form Event Bindings


Document.onmousedown=scrollwindow;

Elements


Document. Forms. Elements[index]


Object Binding Events


Number of Plug-ins


Navigator.plugins

Take variable type


typeof ($js _libpath) = = "Undefined"

Drop down box


Dropdown box. options[Index]
Drop-down box. Options.length

Finding objects


Document.getelementsbyname ("R1");
document.getElementById (ID);


Timing


Uncode Code


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";

Show side


Display:inline



Hide Focus


Hidefocus=true

Wrap line by width


Style= "Word-break:break-all"



Auto Refresh


<meta http-equiv= "Refresh" content= "8; Url=http://c98.yeah.net ">



Simple Mail


<a href= "Mailto:aaa@bbb.com?subject=ccc&body=xxxyyy" >

Quick go to position


Obj.scrollintoview (True)



Anchor


<a name= "a" >
<a href= "#first" >anchors</a>

Page Pass Parameters


Location.search ();

Can be edited


Obj.contenteditable=true

Execute menu command


Obj.execcommand

Double Byte character


/[^\x00-\xff]/

Chinese characters


/[\u4e00-\u9fa5]/

Wrap the English string beyond 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.cssText

HTML tags


Document.documentElement.innerHTML

First Style label


Document.stylesheets[0]

The first style in the Styles tab


Document.stylesheets[0].rules[0]

Prevents clicking on the empty link, the page is often reset to the top of the page.


<a href= "javascript:function ()" >word</a>

Previous page Source


Asp:
Request.ServerVariables ("Http_referer")
Javascript:
Document.referrer

Free memory


CollectGarbage ();

No right key


Document.oncontextmenu = function () {return false;}

Prohibit save


<noscript><iframe src= "*.htm" ></iframe></noscript>

Prohibit selection


<body oncontextmenu= ' return false ' ondragstart= ' return false ' Onselectstart = ' return False '
onselect= "Document.selection.empty" oncopy= "Document.selection.empty)" onbeforecopy=
' Return false ' return false ' >

Address bar icon


<link rel= "Shortcut Icon" href= "Favicon.ico" >

Favicon.ico name best unchanged 16*16 16 color, put the virtual directory root directory
Favorites Bar Icon

<link rel= "Bookmark" href= "Favicon.ico" >


View Source


Close IME


<input style= "ime-mode:disabled" >

Auto Select All


<input type=text name=text1 value= "123" >

Enter to move the cursor to the next input box


<input >

Default values for text boxes


<input type=text value= "123" >

Title Line Wrap


Obj.title = "123 SDFs"

The microseconds represented by the acquisition of time


var n1 = new Date ("2004-10-10". Replace (/-/g, "\/")). GetTime ()

Whether the window is closed


Win.closed

CheckBox flat


<input type=checkbox style= "Position:absolute; Clip:rect (5px 15px 15px 5px) ">

Get the selection


Document.selection.createRange (). Duplicate (). Text

Auto Completion function


<input Type=text autocomplete=on> to open this feature
<input type=text autocomplete=off> Turn off this feature

Window maximization


<body >

No Close button IE


window.open ("aa.htm", "Meizz", "fullscreen=7");

Unified Coding/Decoding


Alert (decodeuricomponent encodeuricomponent ("http://hello. com?as= hehe"))
encodeURIComponent to ":", "/", ";" and "?" Also encoded


Table row indicates


Various sizes


s + = "\ r \ n page visible area wide:" + document.body.clientWidth;
s + + "\ r \ n page visible Area High:" + document.body.clientHeight;
s + + "\ r \ n" page visible area High: "+ Document.body.offsetWeight +" (including the edge of the width);
s + + "\ r \ n" page visible area High: "+ Document.body.offsetHeight +" (including the edge of the width);
s + + "\ r \ n page text full text width:" + document.body.scrollWidth;
s + + "\ r \ n" page full text High: "+ document.body.scrollHeight;
s + + "\ r \ n Pages are rolled High:" + document.body.scrollTop;
s + + "\ r \ n page is rolled to the left:" + document.body.scrollLeft;
s + + "\ r \ n Page body part:" + window.screentop;
s + + "\ r \ n Page body part left:" + window.screenleft;
s + + "\ r \ n screen resolution of High:" + window.screen.height;
s + + "\ r \ n Screen resolution width:" + window.screen.width;
s + = "\ r \ n Screen available working area height:" + window.screen.availHeight;
s + = "\ r \ n Screen available working area width:" + window.screen.availWidth;

Do not 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 match


Matching regular expressions for Chinese characters: [\U4E00-\U9FA5]
Match Double-byte characters (including Chinese characters): [^\x00-\xff]
A regular expression that matches a blank row: \n[\s|] *\r
Regular expression matching HTML tags:/< (. *) >.*<\/\1>|< (. *) \/>/
Matching a regular expression with a trailing space: (^\s*) | (\s*$) (Trim function like VBScript)
Regular expression matching an email address: \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *
A regular expression that matches URL URLs: http://([\w-]+\.) +[\w-]+ (/[\w-/?%&=]*)?

Here is an example:
Use regular expressions to restrict the entry of text boxes in a Web page's form:


//Erase image Toolbar
[/html]
 
or
<meta http-equiv= "Imagetoolbar" content= "no" >
[/html]
//Get absolute position of control (1)
<script language= "javascript" >  <br/>
function GetOffset (e) <br/>
{  <br/>
 var t=e.offsettop;  <br/>
 var l=e.offsetleft;  <br/
 while (e=e.offsetparent) <br/>
 {  <br/>
  t+=e.offsettop;  <BR/>
  l+=e.offsetleft;  <br/>
 }  <br/>
 var rec = new Array (1) ; <br/>
 rec[0]  = t; <br/>
 rec[1] = l; <br/>
 return rec <br/>
}  <br/>
</script> <br/>
 [ctrl+a All Select prompts: You can modify some of the code first, and then press Run]
///Get the absolute position of the control (2)


Orect = Obj.getboundingclientrect ();
Orect.left
Orect.

Print page pagination


<p style= "Page-break-after:always" >page1</p>
<p style= "Page-break-after:always" >page2</p>

Set 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 setup > <br/>
<input Type=button value= Print 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 = "Live Left display &b Center display &b Right Display page number, page &p/Total &p page" <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" > <br/>
<input Type=button value= "Page Setup" > <br/>
<input Type=button value= "Print preview" ><br> <br/>
<a href= "http://www.meadroid.com/scriptx/docs/printdoc.htm?static" target=_blank> specific use manual, more information, point here </a> <br/>
[Ctrl + A ALL SELECT hint: You can modify some of the code, and then run]
Print preview with self


WEBBROWSER.EXECWB (1,1) Open
WEB.EXECWB (2,1) closes all of the IE windows now and opens a new window
WEB.EXECWB (4,1) Save Web page
WEB.EXECWB (6,1) printing
WEB.EXECWB (7,1) Print preview
WEB.EXECWB (8,1) print Page Setup
WEB.EXECWB (10,1) view page Properties
WEB.EXECWB (15,1) seems to be withdrawn, pending confirmation
WEB.EXECWB (17,1) Select all
WEB.EXECWB (22,1) Refresh
WEB.EXECWB (45,1) Close the form without prompting
<style media=print>
. Noprint{display:none;} <!--hide nonprinting 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= Direct Printing ONCLICK=DOCUMENT.ALL.WEBBROWSER.EXECWB (6,6) >
<input Type=button value= Page Setup ONCLICK=DOCUMENT.ALL.WEBBROWSER.EXECWB (8,1) >
</p>
<p> <input Type=button value= Print preview ONCLICK=DOCUMENT.ALL.WEBBROWSER.EXECWB (7,1) >
</center>


Remove headers and footers when printed
<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 to print with an empty <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 footer as 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= "empty page number" Onclick=pagesetup_null () > <br/>
<input type= "button" value= "Restore page number" Onclick=pagesetup_default () > <br/>
[Ctrl + A ALL SELECT hint: You can modify some of the code, and then run]
No-mode prompt box


function Modelessalert (MSG)
{
Window.showmodelessdialog ("Javascript:alert" ("+escape" (MSG) + "\"); Window.close (); "," "," STATUS:NO;RESIZABLE:NO; help:no;dialogheight:height:30px;dialogheight:40px; ");
}


Download files


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 ("Download URL error!");
}
Window.confirm ("Download complete.");
}

Verify that 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 if a Web page 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;
}


Prohibit FSO


1. Logout Component
regsvr32/u Scrrun.dll
2. Modify ProgID
Hkey_classes_root\scripting.filesystemobject
Scripting.FileSystemObject
3. For users who use object, modify the hkey_classes_root\scripting.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.