Dozens of small examples commonly used in javascript

Source: Internet
Author: User

 

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. prohibit right-click actions

<Script Language = "Javascript">
Function click () {if (event. button = 2 | event. button = 3)
{
Alert ('prohibit right-clicking ');
}
Document. // -->
</Script>

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.
}
</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.3ydesign.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. The scrolling information is displayed in the status bar of the window.
(1) Add code to the BODY
<Script language = "javascript">
Var msg = "welcome to the website resources network. Here you will have some gains! ";
Var I = 1
Function scroll ()
{
Mess = msg. substring (I, msg. length) + "" + msg. substring (0, I)
Window. status = mess
I ++;
If (I> = msg. length) I = 1; // you can specify to keep scrolling.
SetTimeout ("scroll ()", 200); // you can specify the scroll speed.
}
</Script>
(2) In the BODY Tag:
<Body onload = "scroll ()">

19. Add the current time to the page

<Script language = "javascript">
Tdy = new Date ();
Document. write ("current time:", tdy. getHours ());
Document. write (":", tdy. getMinutes ());
Document. write (":", tdy. getSeconds ());
</Script>

20. forward and backward buttons
<Input type = "button" value = "<-" onclick = "history. go (-1)">
<Input type = "button" value = "->" onclick = "history. go (1)">


21. Date of last modification
<Script language = "javascript">
Document. write ("Last edit date of this page :");
Document. write (document. lastModified)
</Script>


22. forward and backward buttons
<Font onclick = "history. go (-1)"> previous page </font>
<Font onclick = "history. go (-2)"> previous two pages </font>
<Font onclick = "history. go (-3)"> first three pages </font>
<Font onclick = "history. go (1)"> next page </font>
<Font onclick = "history. go (2)"> last two pages </font>
<Font onclick = "history. go (3)"> last three pages </font>
You can also set back and move forward multiple steps.

23. mouse events
<A href = "MAILTO: webmaster@jzzy.com" return true "> mailbox </A>
Move the mouse up to display warning information


24. Obtain browser attributes
Navigator. appCodename = undefinednavigator. appName = Microsoft Internet Explorernavigator. appVersion = 4.0 (compatible; MSIE 5.0; Windows 98; DigExt) navigator. appAgent = undefined

25. Print the entire page
<Font onClick = "javascript: window. print ()"> print this page </font>

26. View Source Code
<Input TYPE = "button" NAME = "view" value = "view source code on this page" onClick = "window. location = "view-source:" + window. location. href "class =" pt9 ">

27. Refresh the page
<Font onClick = "history. go (0)"> refresh this page </font>

28. Background Color Conversion
<Input TYPE = "button" value = "background color conversion" onClick = "BgButton ()">
<Script> function BgButton ()
{
If (document. bgColor = #00 ffff)
{
Document. bgColor = # ffffff;
}
Else {document. bgColor = #00 ffff;
}
}
</Script>

29. information displayed on the Title
<Script language = "javascript1.2">
<! -- Hide
Document. title = "Today is Sunday"
// -- Hide -->
</Script>

 

> MAILTO: webmaster@jzzy.com "return true"> mailbox </A>
Move the mouse up to display warning information


24. Obtain browser attributes
Navigator. appCodename = undefinednavigator. appName = Microsoft Internet Explorernavigator. appVersion = 4.0 (compatible; MSIE 5.0; Windows 98; DigExt) navigator. appAgent = undefined

25. Print the entire page
<Font onClick = "javascript: window. print ()"> print this page </font>

26. View Source Code
<Input TYPE = "button" NAME = "view" value = "view source code on this page" onClick = "window. location = "view-source:" + window. location. href "class =" pt9 ">

27. Refresh the page
<Font onClick = "history. go (0)"> refresh this page </font>

28. Background Color Conversion
<Input TYPE = "button" value = "background color conversion" onClick = "BgButton ()">
<Script> function BgButton ()
{
If (document. bgColor = #00 ffff)
{
Document. bgColor = # ffffff;
}
Else {document. bgColor = #00 ffff;
}
}
</Script>

29. information displayed on the Title
<Script language = "javascript1.2">
<! -- Hide
Document. title = "Today is Sunday"
// -- Hide -->
</Script>

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.