40 Useful JavaScript Tips

The following are javascript, and on the web site developers will be helpful.

1. Oncontextmenu = "window.event.returnValue = false" will completely shield Right
<table border oncontextmenu=return(false)> <td> no </ table> can be used for Table

2. <body Onselectstart="return false"> deselect to prevent copying

3. Onpaste = "return false" are not allowed to paste

4. Oncopy = "return false;" oncut = "return false;" prevent copying

5. <link Rel="Shortcut Icon" href="favicon.ico"> IE address bar, replace their icons before

6. <link Rel="Bookmark" href="favicon.ico"> can be shown in the Favorites icon on your

7. <input> Close input method

8. Always with the framework
<script language="JavaScript"> <! -
if (window == top) top.location.href = "frames.htm"; / / frames.htm the framework of the page
/ / --></ Script>

9. Prevention of the frame
if (top.location! = self.location) top.location = self.location;
/ / --></ SCRIPT>

10. Page will not be saved as
<noscript> <iframe src="/blog/*.html> "; </ iframe> </ noscript>

11. <Input type = button value = View page source code
OnClick = "window.location =" View-Source: "+" http://www.williamlong.info "">

12. Deletion confirmation
<a href="javascript:if(confirm(" Are you sure you want to delete? ?"))location="boos.asp?&areyou= Delete &page=1""> delete </ a>

13. To 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)
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
msgbox "top =" & t & chr (13) & "left =" & l, 64, "get control of the position"
end function
--></ Script>

14. Cursor is parked in the text of the final 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">

15. Determine the source of previous

16. Minimize, maximize, close window
<param name="Command" value="Minimize"> </ object>
<param name="Command" value="Maximize"> </ object>
<PARAM NAME="Command" VALUE="Close"> </ OBJECT>
<Input type = button value = minimum
<Input type = button value = maximum
<Input type = button value = Close this case applies to IE

17. Shielding function keys Shift, Alt, Ctrl
function look () (
if (event.shiftKey)
alert ("prohibited by Shift key!"); / / can be replaced by ALT CTRL
document.onkeydown = look;
</ Script>

18. Page 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 forms no Ao Tugan?
<input type=text>
<input type=text> </ textarea>

20. <div> <span> & <layer> The difference?
<div> (division) used to define a large section of the page elements, will have to change jobs
<span> used to define the elements within the same line, with the only difference is that <div> not produce switch
<layer> mark is ns, ie does not support the equivalent <div>

21. So that is always at the top of the pop-up window:

22. Do not scroll?
So there is no vertical bar:
</ Body>
So there is no bar:
</ Body>
2 are removed? More simple
<body scroll="no">
</ Body>

23. How to remove the image link is clicked, the image around the dotted line?
<a href="#"> <img src="/blog/logo.jpg" border=0> </ a>

24. E-mail address to submit the form
<Form name = "Form1" method = "post" action = " mailto :****@***. COM "enctype =" text / plain ">
<input type=submit>
</ Form>

25. In open child window parent window to refresh the code in how to write?
window.opener.location.reload ()

26. How do I set the size to open the page
<body onload="top.resizeTo(300,200);">
Open the page position <body onload="top.moveBy(300,200);">

27. In the page how to join the background image is not paved, pull the page background image fixed
(Background-image: url (/ blog / logo.gif); background-repeat: no-repeat;
background-position: center; background-attachment: fixed)

28. Checking whether a string is composed entirely of figures
<script language="Javascript"> <! -
function checkNum (str) (return str.match (/ / D /) == null)
alert (checkNum ("1232142141"))
alert (checkNum ("123214214a1"))
/ / --></ Script>

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

30. How to judge whether the characters
if (/[^/ x00-/xff] / g.test (s)) alert ("with character");
else alert ("all characters");

31.TEXTAREA adaptive number of text rows
<textarea rows=1 name=s1 cols=27 onpropertychange="this.style.posHeight=this.scrollHeight">
</ Textarea>

32. Date minus the number of days equal to the second date
<script language=Javascript>
function cc (dd, dadd)
/ / Can add error handling
var a = new Date (dd)
a = a.valueOf ()
a = a - dadd * 24 * 60 * 60 * 1000
a = new Date (a)
alert (a.getFullYear () + "on" + (a.getMonth () + 1) + "months" + a.getDate () + "Day")
cc ("12/23/2002", 2)
</ Script>

33. Chose which Radio
<HTML> <script Language="vbscript">
function checkme ()
for each ob in radio1
if ob.checked then window.alert ob.value
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">
</ BODY> </ HTML>

34. Script never wrong
<! - Hide
function killErrors () (
return true;
window.onerror = killErrors;
/ / ->

35.ENTER keys to move the cursor to the next input box

<input onkeydown="if(event.keyCode==13)event.keyCode=9">

36. Detection of a site's link speed:
The following code to <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="/blog/ Test ......>" = "<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)

b + +
function run () (for (var i = 1; i <autourl.length; i + +) document.write ("<img src = http://" + autourl +"/"+ Math.random () + "width = 1 height = 1

OnError = auto (" http:// "+ autourl +"")>")}
run () </ script>

37. A variety of styles cursor
auto: standard cursor
default: standard arrow
hand: hand cursor
wait: wait cursor
text: I-shaped cursor
vertical-text: level I-shaped cursor
no-drop: can not drag the cursor
not-allowed: Invalid Cursor
help:? Help Cursor
all-scroll: the Direction Triangle
move: mobile standard
crosshair: Cross Standard

38. Page entry and exit of the effects into the page <meta http-equiv="Page-Enter" content="revealTrans(duration=x, transition=y)">
Introduction page <meta http-equiv="Page-Exit" content="revealTrans(duration=x, transition=y)">
This is the page is loaded and transferred out to some of the effects. that the duration of effect duration, in seconds. What kind of effects that the use of transition with value 1-23:
0 1 rectangular narrow narrow rectangular expansion of 2 Round 3 Round 4 bottom to expand on to the next refresh refresh 5 6 7 left to right and right to left refresh refresh 8 9 horizontal blinds vertical blinds horizontal blinds 11, 10 dislocation dislocation vertical blinds 12 spread among 13 or so to refresh refresh about 14 to 15 among the middle to the upper and lower levels of 16 to 17 among the lower right to upper left 18 19 left to upper right to lower left lower right lower left to upper right 20 vertical bar 21 bar 22, more than 22 species of 23 randomly selected a seed

39. In a specified time jump
<META Http-equiv=V="REFRESH" content="5;URL=http://www.williamlong.info">

40. Web page is to be retrieved
<meta name="ROBOTS" content=" The property value ">
Where property values are the following:
Property value is "all": the files will be retrieved, and the page links may be inquiries;
Property value is "none": file not being retrieved, and do not check the links page;
Property value is "index": the files will be retrieved;
Property value is "follow": check the links page;
Attribute value "noindex": file does not retrieve, but can be check links;
Attribute value "nofollow": file is not retrieved, but can check the links page.

Maximize the window?
<script language="JavaScript">
<! -
self.moveTo (0,0)
self.resizeTo (screen.availWidth, screen.availHeight)
</ Script>

Solve the problem: As the layer and the priority between the drop-down box is: drop-down box> layer, and therefore shows the time because the order of priority issues which will appear above. (If several elements are level, then, we can layer z-index property to set) solution is: to put a layer of priority higher than the drop-down box element (iframe), so as to solve this problem! Specific solutions to the code as follows:


<tr> <td> item 1 </ td> </ tr>
<tr> <td> item 2 </ td> </ tr>
<tr> <td> item 3 </ td> </ tr>
<tr> <td> item 4 </ td> </ tr>
<tr> <td> item 5 </ td> </ tr>
</ Table>
<iframe src="/blog/javascript:false"> </ iframe>
</ Div>

<a href="#"> menu </ a>

<select> <option> A form selection list </ option> </ select>
</ Form>

Input box is also very beautiful can do
<div align="center"> <input type="hidden" name="hao" value="yes">
Outgoing number: <input
name = answer
Did not answer several questions: <input
name = unanswer
Total points:
name = score
name = xgjg

<input"FONT-FAMILY: Arial ; FONT-SIZE: 9pt" type=button value= View results name="button">
<input type="reset" name="Submit" value=" Redo ">
</ Div>

Note: Modify <body> shall be open for the <body onload="max.Click()"> largest

The window, and if instead <body onload="min.Click()"> becomes a window open on the minimum

<object type="application/x-oleobject" classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11">
<param name="Command" value="Minimize">
</ Object> <object type="application/x-oleobject" classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11">
<param name="Command" value="Maximize">
</ Object>
</ Body>

Automatically refresh the page (note)

When you do a Web page, is not sometimes want you to constantly refresh the page automatically, or over a period of time automatically jump to another page to set your own? In fact, to achieve this effect very simple, and this effect can not even call it special effects. You just put the following code to your page on it.

1, the page automatically refresh: the following code to <head> area <meta http-equiv="refresh" content="20">, of which 20 refer to a page refresh every 20 seconds.

2, page frames: the following code to <head> area <meta http-equiv="refresh" content="20;url=http://www.williamlong.info">, of which 20 means 20 seconds separated After the jump to http://www.williamlong.info page.

Automatically close the page

5000 is the time <body onLoad="setTimeout(window.close, 5000)">

Pop-up window closes automatically

After 10 seconds the pop-up window closes automatically

Note: In the new tan.htm the body, we should add <onLoad="closeit()">

<script language="JavaScript">

<! -

var gt = unescape ('% 3e');

var popup = null;

var over = "Launch Pop-up Navigator";

popup = window.open ('', 'popupnav', 'width = 225, height = 235, resizable = 1, scrollbars = auto');

if (popup! = null) (

if (popup.opener == null) (

popup.opener = self;


popup.location.href = 'tan.htm';


/ / ->

</ Script>
<body> Note: this code is in the new file
<script language="JavaScript">

function closeit ()

</ Script>

This is not <iframe> (cited) Yeah. Is a direct call. The following code to <body> region

<object type="text/x-scriptlet" width="800" height="1000" data="../index.htm">
</ Object>

This article comes from CSDN blog, reproduced, please indicate the source: http://blog.csdn.net/lin0shyi1/archive/2009/03/17/3995263.aspx

分类:Web 时间:2010-09-01 人气:270
blog comments powered by Disqus


iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.647 (s). 10 q(s)