Common JavaScript methods (included)

Source: Internet
Author: User

1. oncontextmenu = "window. event. ReturnValue = false "will completely block the right mouse button
<Table border oncontextmenu = return (false)><Td> no </table>
2. <body onselectstart = "return false"> cancel selection to prevent Replication

3. onpaste = "return false" Do not paste

4. oncopy = "return false;" oncut = "return false;" Prevent Replication

5. <link rel = "Shortcut Icon" href = "favicon. ico"> replace it with your own Icon in front of the IE Address Bar

6. <link rel = "Bookmark" href = "favicon. ico"> your Icons can be displayed in favorites.

7. <input style = "ime-mode: disabled"> disable the input method.

8. Always carry the framework
<Script language = "JavaScript"> <! --
If (window = top) top. location. href = "frames.htm"; // frames.htm is the frame webpage.
// --> </Script>

9. Prevent frame
<Script language = JAVASCRIPT> <! --
If (top. location! = Self. location) top. location= Self. location;
// --> </SCRIPT>

10. The webpage cannot be saved
<Noscript> <iframe src = *. html> </iframe> </noscript>

11. <input type = button value = view webpage source code
Onclick = "window. location =" view-source: "+" http://www.pconline.com.cn "">

12. Confirm deletion
<A href = "javascript: if (confirm ("Are you sure you want to delete it? ") Location =" boos. asp?& Areyou = Delete & page = 1 "> Delete </a>

13. Obtain the absolute position of the control
// Javascript
<Script language = "Javascript">
Function getIE (e ){
Var t = e. offsetTop;
Var l = e. offsetLeft;
While (e = e. offsetParent ){
T + = e. offsetTop;
L + = e. offsetLeft;
}
Alert ("top =" + t + "/nleft =" + l );
}
</Script>

// VBScript
<Script language = "VBScript"> <! --
Function getIE ()
Dim t, l, a, B
Set a = document. all. img1
T = document. all. img1.offsetTop
L = document. all. img1.offsetLeft
While a. tagName <> "BODY"
Set a = a. offsetParent
T = t + a. offsetTop
L = l + a. offsetLeft
Wend
Msgbox "top =" & t & chr (13) & "left =" & l, 64,"Get the widget location"
End function
--> </Script>

14. The cursor stops at the end of the text box.
<Script language = "javascript">
Function cc ()
{
Var e = event. srcElement;
Var r = e. createTextRange ();
R. moveStart ("character", e. Value. length );
R. collapse (true );
R. select ();
}
</Script>
<Input type = text name = text1 value = "123" onfocus = "cc ()">

15. determine the source of the previous page
Javascript:
Document. referrer

16. Minimize, maximize, and close the window
<Object id = hh1 classid = "clsid: ADB880A6-D8FF-11CF-9377-00AA003B7A11 ">
<Param name = "Command" value = "Minimize"> </object>
<Object id = hh2 classid = "clsid: ADB880A6-D8FF-11CF-9377-00AA003B7A11 ">
<Param name = "Command" value = "Maximize"> </object>
<OBJECT id = hh3 classid = "clsid: adb880a6-d8ff-11cf-9377-00aa003b7a11 ">
<Param name = "Command" VALUE = "Close"> </OBJECT>
<Input type = button value = minimized onclick = hh1.Click ()>
<Input type = button value = maximized onclick = hh2.Click ()>
<Input type = button value = Disable onclick = hh3.Click ()>
This example applies to IE

17. Block function keys Shift, Alt, Ctrl
<Script>
Function look (){
If (event. shiftKey)
Alert ("do not press Shift! "); // You can change it to ALT CTRL
}
Document. onkeydown = look;
</Script>

18. The webpage will not be cached
<META HTTP-EQUIV = "pragma" CONTENT = "no-cache">
<META HTTP-EQUIV = "Cache-Control" CONTENT = "no-cache, must-revalidate">
<META HTTP-EQUIV = "expires" CONTENT = "Wed, 26 Feb 1997 08:21:57 GMT">
Or <META HTTP-EQUIV = "expires" CONTENT = "0">

19. How to Make the form feel free of attention?
<Input type = text style = "border: 1 solid #000000">
Or
<Input type = text style = "border-left: none; border-right: none; border-top: none; border-bottom:

1 solid #000000 "> </textarea>

20. What are the differences between <div> <span> & <layer>?
<Div> (division) is used to define the page elements of a large segment., Will generate a transposed line
<Span> defines the elements in the same row.> The only difference is that no forwarding is generated.
<Layer> is the ns tag, which is not supported by ie, which is equivalent to <div>

21. Make the pop-up window always on the top:
<Body onblur = "this. focus ();">

22. Do not use a scroll bar?
Make the vertical bar not:
<Body style = "overflow: scroll; overflow-Y: hidden ">
</Body>
Make the horizontal bar unavailable:
<Body style = "overflow: scroll; overflow-X: hidden ">
</Body>
Remove both of them? Simpler
<Body scroll = "no">
</Body>

23. How do I remove the dotted line around the image after clicking the image link?
<A href = "#" onFocus = "this. blur ()"> </a>

24. email processing submission form
<Form name = "form1" method = "post" action = "mailto: ***** @ *****. com" enctype = "text/plain">
<Input type = submit>
</Form>

25. How to refresh the code of the parent window in the opened Child Window?
Window. opener. location. reload ()

26. How to set the page size
<Body onload = "top. resizeTo (300,200 );">
<Body onload = "top. moveBy (300,200);">

27. How to add a background image that is not full? The background image does not move when pulling the page
<STYLE>
Body
{Background-image: url(logo.gif); Background-repeat: no-repeat;
Background-position: center; Background-attachment: fixed}
</STYLE>

28. check whether a string is composed of digits.
<Script language = "Javascript"> <! --
Function checkNum (str) {return str. match (// D/) = null}
Alert (checkNum ("1232142141 "))
Alert (checkNum ("123214214a1 "))
// --> </Script>

29. Get the size of a window
Document. body. clientWidth; document. body. clientHeight

30. How to determine whether it is a character
If (/[^/x00-/xff]/g. test (s) alert ("containing Chinese characters ");
Else alert ("All characters ");

31. Number of lines of TEXTAREA adaptive text
<Textarea rows = 1 name = s1 cols = 27 onpropertychange = "this. style. PosHeight = this. scrollHeight ">
</Textarea>

32. The date minus the number of days equals to the second date
<Script language = Javascript>
Function cc (dd, dadd)
{
// Add error handling.
Var a = new Date (dd)
A = a. valueOf ()
A = a-dadd * 24x60*60*1000
A = new Date ()
Alert (a. getFullYear () + "year" + (a. getMonth () + 1) + "month" + a. getDate () + "day ")
}
Cc ("12/23/2002", 2)
</Script>

33. Which Radio is selected
<HTML> <script language = "vbscript">
Function checkme ()
For each ob in radio1
If ob. checked then window. alert ob. value
Next
End function
</Script> <BODY>
<INPUT name = "radio1" type = "radio" value = "style" checked> Style
<INPUT name = "radio1" type = "radio" value = "barcode"> Barcode
<INPUT type = "button" value = "check" onclick = "checkme ()">
</BODY> </HTML>

34. Script never fails
<Script language = "JavaScript">
<! -- Hide
Function killErrors (){
Return true;
}
Window. onerror = killErrors;
// -->
</SCRIPT>

35. Press ENTER to move the cursor to the next input box.
<Input onkeydown = "if (event. keyCode == 13) event. keyCode = 9 ">

36. Check the link speed of a Website:
Add the following code to the <body> area:
<Script language = Javascript>
Tim = 1
SetInterval ("tim + +", 100)
B = 1
Var autourl = new Array ()
Autourl [1] = "www.njcatv.net"
Autourl [2] = "javacool.3322.net"
Autourl [3] = "www.sina.com.cn"
Autourl [4] = "www.nuaa.edu.cn"
Autourl [5] = "www.cctv.com"
Function butt (){
Document. write ("<form name = autof> ")
For (var I = 1; I <autourl. length; I ++)
Document. write ("<input type = text name = txt" + I + "size = 10 value = testing...> = "<Input type = text
Name = url "+ I +" size = 40 >=" <input type = button value = GO

Onclick = window. open (this. form. Url "+ I +". value)> <br> ")
Document. write ("<input type = submit value = Refresh> </form> ")
}
Butt ()
Function auto (url ){
Document. forms [0] ["url" + B]. Value = url
If (tim> 200)
{Document. forms [0] ["txt" + B]. Value = "link timeout "}
Else
{Document. forms [0] ["txt" + B]. Value = "time" + tim/10 + "second "}
B ++
}
Function run () {for (var I = 1; I <autourl. length; I ++) Document. write ("

Onerror = auto ("http: //" + autourl + "")> ")}
Run () </script>

37. Various styles of cursor
Auto: Standard cursor
Default: Standard arrow
Hand: hand-shaped cursor
Wait: waiting for the cursor
Text: I-shaped cursor
Vertical-text: horizontal I-shaped cursor
No-drop: Do not drag the cursor
Not-allowed: Invalid cursor
Help :? Help cursor
All-scroll: triangle direction indicator
Move: move mark
Crosshair: Cross logo
E-resize
N-resize
Nw-resize
W-resize
S-resize
Se-resize
Sw-resize

38. Special Effects for entering and exiting the page
Go to the <meta http-equiv = "Page-Enter" content = "revealTrans (duration= X, transition = y) ">
Launch Page <meta http-equiv = "Page-Exit" content = "revealTrans (duration= X, transition = y) ">
This is some special effects of page loading and calling. Duration indicates the special effectDuration, in seconds. Transition indicates which special effect to use, Value range: 1-23:
0 rectangular narrowing
1. rectangular enlargement
2 rounded down
3. Circular Expansion
4. Refresh from bottom to top
Refresh from top to bottom
6-left-right refresh
7. right-to-left refresh
8 vertical blinds
9 horizontal blinds
10 misplaced horizontal blinds
11 misplaced vertical blinds
Spread
Refresh from around 13 to the center
14. Refresh from center to left
15.
16 up to the middle
17 bottom right to top left
18 top right to bottom left
19 upper left to lower right
20 bottom left to top right
21 horizontal bars
22 vertical bars
23 or more 22 types of random selection

39. Jump within the specified time
<META http-equiv = V = "REFRESH" content = "5; URL = http://www.51js.com">

40. Whether the webpage is retrieved
<Meta name = "ROBOTS" content = "attribute value">
The attribute values include the following:
Attribute Value: "all": the file will be retrieved and the link on the page can be queried;
Attribute Value: "none": the file is not retrieved and the link on the page is not queried;
The property value is "index": the file will be retrieved;
The property value is "follow": the link on the query page;
Attribute Value: "noindex": the file is not retrieved, but can be queried;
The property value is "nofollow": the file is not retrieved, but the link on the page can be queried.

41. (1) div tag Control

The div tag is different from the span tag. The div is a layer block.Span is a line. We can see the difference in the demo below. Let's take a look at a piece of control dIv code.

<Script language = "javascript">
Function chageDiv (number)
{
If (number = 1 ){
Document. getElementById ("div1"). InnerHTML = "value: 1 ";
}
If (number = 2 ){
Document. getElementById ("div1"). InnerHTML = "value: 2 ";
}
}
</Script>

DIV block test: <div id = "div1"> default value </div>

<A href = "#" onClick = "chageDiv (1)"> change the value to 1. </a>
<A href = "#" onClick = "chageDiv (2)"> change the value to 2 </a>

When running, click "change value to 1", the "Default Value" will be changed.It is set to "value 1", but note that the interface will find "DIV test:"And" Default Value "are displayed in two rows, because DIV is displayed by block.

(2) span control

Similar to div, but it is displayed by line. See the following code:

Function chageSpan (number)
{
If (number = 1 ){
Document. getElementById ("span1"). InnerHTML =" value: 1 ";
}
If (number = 2 ){
Document. getElementById ("span1"). InnerHTML =" value: 2 ";
}
}
</Script>
Span row test:
<Span id = "span1"> default value </span> <br>
<A href = "#" onClick = "chageSpan (1)"> change the value to 1 </A>
<A href = "#" onClick = "chageSpan (2)"> change the value to 2 </A>

When you click "change value to 1", the "Default Value" will be changed to "value to 1","Span row test" and "Default Value" are displayed in the same rowIs different from DIV.

It is worth noting that, whether it is div or spanThe following names are defined by id, not by name like a form..

(3) confirm confirmation prompt box Creation

When we want to perform a dangerous operation, such as deleting a contentIn this case, you should give the user a prompt to avoid making mistakes.. Generally, the confirm () function is used for processing., Submit a parameter for it as the displayed information prompt, then a dialog will pop up during accessBox, if you click "OK", the function will be changed to return true, click "cancel""Put false back. We use two methods to control this feature.No.

Check the Code:

<Script language = "javascript">
Function accessNeteasy ()
{
If (confirm ('You really want to visit Netease news? ')){
Location = 'HTTP: // calendar. eyou. eyou ';
}
}
Function accessSina ()
{
If (confirm ('Are you sure you want to visit Sina news? ')){
Return true;
} Else {
Return false;
}
}
</Script>

Access Method 1:
<A href = "#" onClick = "accessNeteasy ()"> Netease news</A> <br>
Access Method 2:
<A href = "http://news.sina.com.cn" onClick = "return accessSina ()"> Sina news </a>

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.