I have summarized a lot recently.
1 JavaScript direction key capture
<HTML>
<Head>
<Title> invert selection </title>
<Script language = "JavaScript">
Function check ()
{
If (event. keycode = "37 ")
Alert ("you have selected the left button! ");
If (event. keycode = "38 ")
Alert ("you have selected the upper key! ");
If (event. keycode = "39 ")
Alert ("right-click! ");
If (event. keycode = "40 ")
Alert ("you have selected the next key! ");
}
</SCRIPT>
</Head>
<Body onkeydown = "check ()">
</Body>
</Html>
2. Protect the webpage code from being visible
<SCRIPT>
Function clear ()
{
Source = Document. Body. innerhtml; // obtain the original content of the document.
Document. open (); // open the document
Document. Write ("code blocked"); // output the prompt content
Document. Close (); // close the document
Document. Title = "unable to see source code"; // document title
Document. Body. innerhtml = source; // re-write the old content
}
</SCRIPT>
</Head>
<Body onload = clear ()>
3. determine the number of Chinese characters in a string.
<Script language = "JavaScript">
Function CAL (STR)
{
Re =/[\ u4e00-\ u9fa5]/g; // test the Regular Expression of Chinese Characters
If (Re. Test (STR) // use regular expressions to determine whether Chinese characters exist.
Return Str. Match (re). Length // returns the number of Chinese Characters
Else
Return 0
}
</SCRIPT>
<Input onblur = "alert (CAL (this. Value)">
4. Protection against F5 refreshing
<SCRIPT>
Document. onkeydown = noway; // bind a form loading event
Function noway (){
If (event. keycode = 116) {// you can use the key value to determine if it is F5.
Event. keycode = 0;
Event. returnvalue = false; // No operation is performed.
}
}
</SCRIPT>
5 Ctrl + enter submit data
<Script language = JavaScript>
// Determine the browser type
Ie = (document. All )? True: false
If (IE)
{
Function ctlent (eventobject)
{
// Obtain the key value entered by the user
If (event. ctrlkey & window. event. keycode = 13)
Export this.doc ument. form1.submit ();}}
}
</SCRIPT>
</Head>
<Body>
<Form action = "http://www.baidu.com" method = "get" name = "form1">
<Textarea Cols = "50" name = "content" rows = "10" Wrap = "virtual" onkeydown = "ctlent ()">
CTRL + enter submit content
</Textarea>
<Input type = submit value = "Submit" name = submit>
</Form>
6 Make the scroll wheel invalid
<Script language = "JavaScript">
Function document. onmousewheel () // wheel event redefinition
{
Return false; // if false is returned, no operation is performed.
}
</SCRIPT>
</Head>