Common JavaScript skills

Source: Internet
Author: User
I have summarized a lot recently.
1 JavaScript direction key capture
<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! ");
<Body onkeydown = "check ()">

2. Protect the webpage code from being visible
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
<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
Return 0
<Input onblur = "alert (CAL (this. Value)">

4. Protection against F5 refreshing
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.

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 ();}}
<Form action = "" method = "get" name = "form1">
<Textarea Cols = "50" name = "content" rows = "10" Wrap = "virtual" onkeydown = "ctlent ()">
CTRL + enter submit content
<Input type = submit value = "Submit" name = submit>

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.

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: 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.