Common JavaScript statement sets (2)

Source: Internet
Author: User

I sorted out the "common JavaScript statement set (1)" last time and thought it was incomplete. Today I read a collection from someone else, so I did it myself.

Added some frequently-used JavaScript statement sets (2 )".

The following lists some commonly used Javascript. You can make these into a class to facilitate repeated calls in the project.

Please add ^_^

 

1. Keep text scrolling

<Marquee> scroll text </marquee>

2. Record and display the last modification time of the webpage

<Script language = JavaScript>
Document. Write ("Last Update time:" + document. lastmodified + "")
</SCRIPT>

3. Close the current window

<A href = "/" onclick = "javascript: window. Close (); Return false;"> close the window </a>

Close the current page in 4.5 seconds

<Script language = "JavaScript">
<! --
SetTimeout ('window. Close (); ', 5000 );
-->
</SCRIPT>

Load the specified webpage in 5.2 seconds

<Head>
<Meta http-equiv = "refresh" content = "2; url = http: // your url">
</Head>

6. Add to favorites

<Script language = "JavaScript">
Function bookmarkit ()
{
Window. External. AddFavorite ('HTTP: // your url ', 'Your website name ')
}
If (document. All) document. Write ('<a href = "#" onclick = "bookmarkit ()"> Add to favorites </a> ')
</SCRIPT>

7. Leave the hyperlink ununderlined

<Style type = "text/CSS">
<! -
A: link {text-Decoration: None}
A: hover {text-Decoration: None}
A: visited {text-Decoration: None}
->
</Style>

8. Move the webpage back at will

<A href = "javascript: history. Go (-x)"> x </a>

// Replace X with the number of pages on which you want to return
// Change "-" to "+" to forward

9. Set this page as the homepage

<Body bgcolor = "# ffffff" text = "#000000">
<! -- URL: http: // your url -->
<A class = "chlnk" style = "cursor: Hand" href
Onclick = "This. style. Behavior = 'url (# default # homepage )';
This. setHomePage ('your website name); "> <font color =" 000000 "size =" 2 "face =" "> set as homepage </font> </a>
</Body>

10. Holiday countdown

<Script language = "JavaScript">
VaR TimeDate = new date ("December 25, 2003 ");
VaR times = "Christmas ";
VaR now = new date ();
VaR date = TimeDate. gettime ()-Now. gettime ();
VaR time = math. Floor (Date/(1000*60*60*24 ));
If (Time> = 0)
Document. Write ("now" + times + "and:" + time + "day") </SCRIPT>

11. click the button to print out the current page

<Script language = "JavaScript">
<! -- Begin
If (window. Print ){
Document. Write ('<form>'
+ '<Input type = button name = print value = "print this page "'
+ 'Onclick = "javascript: window. Print ()"> </form> ');
}
// End -->
</SCRIPT>

12. click 'save as 'on the current page.

<Input type = "button" name = "button" value = "Save this page"
Onclick = "document. All. Button. execwb (4,1)">
<Object ID = "button"
Width = 0
Height = 0
Classid = "CLSID: 8856f961-340a-11d0-a96b-00c04fd705a2">
<Embed width = "0" Height = "0"> </embed>
</Object>

13. display the current system date

<Script language = JavaScript>
Today = new date ();
Function date (){
This. Length = date. Arguments. Length
For (VAR I = 0; I <this. length; I ++)
This [I + 1] = date. Arguments}
VaR d = new date ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday ");
Document. Write (
"<Font color =## 000000 style = 'font-size: 9pt; font-family: '> ",
Today. getyear (), "year", today. getmonth () + 1, "month", today. getdate (), "day ",
D [today. getday () + 1], "</font> ");
</SCRIPT>

14. display different greetings in different time periods

<Script language = "JavaScript">
<! --
VaR text = ""; Day = new date (); time = day. gethours ();
If (Time> = 0) & (time <7 ))
TEXT = "night owl, pay attention to your health! "
If (Time> = 7) & (time <12 ))
TEXT = "today's weather ...... Haha, don't you play? "
If (Time> = 12) & (time <14 ))
TEXT = "lunch break, friends are not used to nap, right ?! "
If (Time> = 14) & (time <18 ))
TEXT = "It's time for afternoon tea. Have a rest! "
If (Time> = 18) & (time <= 22 ))
TEXT = "You're coming again. Don't talk to mm too long! "
If (Time> = 22) & (time <24 ))
TEXT = "It's very late. Pay attention to the rest! "
Document. Write (text)
// --->
</SCRIPT>

15. reflection effect in Water


<Script language = "JavaScript">
Function F1 ()
{
Setinterval ("mdiv.filters.wav E. Phase +=10", 100 );
}
If (document. All)
{
Document. Write ('Style = "filter: Wave (strength = 3, freq = 3, phase = 0, lightstrength = 30) blur () flipv ()"> ')
Window. onload = F1
}
</SCRIPT>

16. The window that grows slowly

<Script language = "JavaScript">
<! --
VaR windowsheight = 100
VaR windowsswidth = 100
VaR numx = 5
Function OpenWindow (thelocation ){
Temploc = thelocation
If
(! (Window. resizeTo & document. All )&&! (Window. resizeTo & document. getelementbyid ))
{
Window. Open (thelocation)
Return
}
Windowsize = Window. Open ("", "", "scrollbars ")
Windowsize. moveTo (0, 0)
Windowsize. resizeTo (100,100)
Tenumxt ()
}
Function tenumxt (){
If (windowsheight> = screen. availHeight-3)
Numx = 0
Windowsize. resizeBy (5, numx)
Windowsheight + = 5
Windowswidth + = 5
If (windowswidth> = screen. Width-5)
{
Windowsize. Location = temploc
Windowsheight = 100
Windowswidth = 100
Numx = 5
Return
}
SetTimeout ("tenumxt ()", 50)
}
// -->
</SCRIPT>
<P> <a href = "javascript: OpenWindow (http://www.xxx.com)"> enter </a>

17. Change the IE icon in the IE Address Bar

We need to first create a 16*16 icon (icon file) and save it as index. ICO. Upload the icon file to the root directory and add the following code between the <LINK rel = "shortcut icon" href = "index. ICO">

18. A dialog box pops up when the cursor points
<Body>
<A href onmouseover = "alert ('pop-up message! ') "> Displayed link text </a>
</Body>

19. Right-click to bring up the Add favorites dialog box
<Body>
<Script language = JavaScript>
If (document. All)
Document. Body. onmousedown = new function ("If (event. Button = 2 | event. Button = 3)
Window. External. AddFavorite ('your website', 'Your website )")
</SCRIPT>
</Body>

20. When you leave the webpage and load the Website, you are prompted to add the site to favorites

It can greatly increase the Website access volume.

I.

? Function favorate (){

? Window. External. AddFavorite ('HTTP: // blog.csdn.net/kanjing', 'Blue space ')

? }
?

2. Add the following code to the mark on the webpage:

OnUnload = favorate ()

If you want to load a webpage, you only need

The above onUnload = favorate () is changed to onload = favorate ()

21. JS:

Copy the following code to the

<Script language = JavaScript>
<! --
VaR MSG = "blue space ";
VaR speed = 300;
VaR msgud = "" + MSG;
Function titlescroll (){
If (msgud. Length msgud = msgud. substring (1, msgud. Length );
Document. Title = msgud. substring (0, MSG. Length );
Window. setTimeout ("titlescroll ()", speed );
}
-->
</SCRIPT>

Add the following code to the webpage body:

<Body onload = "window. setTimeout ('maid () ', 500)">

22. js status bar Information Branch display:

Copy the following code to the

<Script language = JavaScript>
<! --
Function makearray (n ){
This. Length = N;
}
Function stopbanner (){
If (bannerrunning)
Cleartimeout (timerid );
Bannerrunning = false;
}
Function startbanner (){
Stopbanner ();
Showbanner ();
} Function showbanner (){
VaR text = ar [currentmessage];
If (offset <text. Length ){
If (text. charat (offset) = "")
Offset ++;
VaR partialmessage = text. substring (0, offset + 1 );
Window. Status = partialmessage;
Offset ++;
Timerid = setTimeout ("showbanner ()", speed );
Bannerrunning = true;
} Else {
Offset = 0;
Currentmessage ++;
If (currentmessage = arlength)
Currentmessage = 0;
Timerid = setTimeout ("showbanner ()", pause );
Bannerrunning = true;
}
}
VaR speed = 100
VaR pause = 1000
VaR timerid = NULL;
VaR bannerrunning = false;
VaR currentmessage = 0;
VaR offset = 0;
VaR arlength = 3;
VaR AR = new makearray (arlength );
Ar [0] = "blue space ";
Ar [1] = "welcome ";
Ar [2] = "if you have any suggestions, please contact V-blue power ";
-->
</SCRIPT>

Add the following code to the webpage body:

<Body onload = startbanner ()>

23. Your Icons can be displayed in favorites

<LINK rel = "bookmark" href = "favicon. ICO">

24. Always carry the framework

<Script language = "JavaScript"> <! --
If (window = Top) top. Location. href = "frames.htm"; // frames.htm is the frame webpage.
// --> </SCRIPT>

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

26. Number of lines of textarea adaptive text

<Textarea rows = 1 name = S1 Cols = 27 onpropertychange = "This. style. posheight = This. scrollheight">
</Textarea>

27. 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 (" ")}
Run () </SCRIPT>

28. How to change the Mouse shape of a link

You just need to add this code to the link.
Or keep up with the above and write it in CSS.

Style = "cursor: Hand" style = "cursor: crosshair"
Style = "cursor: Text" style = "cursor: Wait"
Style = "cursor: Move" style = "cursor: help"
Style = "cursor: E-resize" style = "cursor: N-resize"
Style = "cursor: NW-resize" style = "cursor: W-resize"
Style = "cursor: S-resize" style = "cursor: Se-resize"
Style = "cursor: Sw-resize"
You only need to add the above Code to the connection or the style area of the page to diversify the mouse.

29. Cookie script records are of great use.

Function get_cookie (name ){
VaR search = Name + "="
VaR returnValue= "";
If (events. Cookie. length> 0 ){
Offset = documents. Cookie. indexof (Search)
If (offset! =-1) {// If Cookie exists
Offset + = search. Length
// SET index of beginningValue 
End = documents. Cookie. indexof (";", offset );
// SET index of end of cookieValue 
If (END =-1)
End = documents. Cookie. length;
ReturnValue= Unescape (events. Cookie. substring (offset, end ))
}
}
Return returnValue;
}
Function loadpopup (){
If (get_cookie ('popped') = ''){
Openpopup ()
Documents. Cookie = "popped = yes"

}
}
Note: The above is the JS Code. Please add the start and end characters on your own.

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.