JavaScript Entry code collation, for the Java script is not familiar to the master only one step away from the person.
JS Getting Started code 2009/04/21 13:59JS Entry Code
Fit to read: Don't know anything about Java Script ~ One step away from mastery
Basics: HTML
Java script just like that. 1: basic knowledge
1 Creating script blocks
1: <script language= "java Script" >
2:java script code goes here
3: </script>
2 Hide Script code
1: <script language= "java Script" >
2: <!--
3:document.write ("Hello");
4://-->
5: </script>
Do not execute related code in browsers that do not support Java script
3 when the browser does not support the display
1: <noscript>
2:hello to the Non-java script browser.
3: </noscript>
4 Linking external script files
1: <script language= "java Script" src= "/" Filename.js "></script>
5 Annotation Scripts
1://This is a comment
2:document.write ("Hello"); This is a comment
3:/*
4:all of this
5:is a Comment
6: */
6 Output to Browser
1:document.write ("<strong>Hello</strong>");
7 Defining variables
1:var myvariable = "some value";
8 String Addition
1:var myString = "String1" + "String2";
9 String Search
1: <script language= "java Script" >
2: <!--
3:var myvariable = "Hello There";
4:var thereplace = Myvariable.search ("there");
5:document.write (Thereplace);
6://-->
7: </script>
10 String Replacement
1:thisvar.replace ("Monday", "Friday");
11 Formatting strings
1: <script language= "java Script" >
2: <!--
3:var myvariable = "Hello There";
4:document.write (Myvariable.big () + "<br>");
5:document.write (Myvariable.blink () + "<br>");
6:document.write (Myvariable.bold () + "<br>");
7:document.write (myvariable.fixed () + "<br>");
8:document.write (Myvariable.fontcolor ("red") + "<br>");
9:document.write (Myvariable.fontsize ("18pt") + "<br>");
10:document.write (Myvariable.italics () + "<br>");
11:document.write (Myvariable.small () + "<br>");
12:document.write (Myvariable.strike () + "<br>");
13:document.write (Myvariable.sub () + "<br>");
14:document.write (Myvariable.sup () + "<br>");
15:document.write (Myvariable.tolowercase () + "<br>");
16:document.write (Myvariable.touppercase () + "<br>");
17:
18:var firststring = "My String";
19:var finalstring = Firststring.bold (). toLowerCase (). FontColor ("Red");
://-->
</script>
12 Creating an array
1: <script language= "java Script" >
2: <!--
3:var myarray = new Array (5);
4:myarray[0] = "Entry";
5:MYARRAY[1] = "Second Entry";
6:MYARRAY[2] = "Third Entry";
7:MYARRAY[3] = "Fourth Entry";
8:myarray[4] = "Fifth Entry";
9:var Anotherarray = new Array ("Entry", "Second Entry", "Third Entry", "Fourth Entry", "fifth Entry");
Ten://-->
One: </script>
13 Array Sorting
1: <script language= "java Script" >
2: <!--
3:var myarray = new Array (5);
4:myarray[0] = "Z";
5:MYARRAY[1] = "C";
6:MYARRAY[2] = "D";
7:MYARRAY[3] = "a";
8:MYARRAY[4] = "Q";
9:document.write (Myarray.sort ());
Ten://-->
One: </script>
14 Split string
1: <script language= "java Script" >
2: <!--
3:var myvariable = "A,b,c,d";
4:var Stringarray = Myvariable.split (",");
5:document.write (Stringarray[0]);
6:document.write (stringarray[1]);
7:document.write (stringarray[2]);
8:document.write (Stringarray[3]);
9://-->
: </script>
15 Pop-up warning message
1: <script language= "java Script" >
2: <!--
3:window.alert ("Hello");
4://-->
5: </script>
16 Pop-up Confirmation box
1: <script language= "java Script" >
2: <!--
3:var result = Window.confirm ("Click OK to continue");
4://-->
5: </script>
17 Defining functions
1: <script language= "java Script" >
2: <!--
3:function Multiple (number1,number2) {
4:var result = Number1 * NUMBER2;
5:return result;
6:}
7://-->
8: </script>
18 Calling JS function
1: <a href= "#" onclick= "functionname ()" >link text</a>
2: <a href= "/" Java Script:functionname "()" >link text</a>
19 Execute function after page load completes
1: <body onload= "functionname" (); >
2:body of the page
3: </body>
20 Piece Judgment
1: <script>
2: <!--
3:var Userchoice = window.confirm ("Choose OK or Cancel");
4:var result = (Userchoice = = True)? "OK": "Cancel";
5:document.write (result);
6://-->
7: </script>
21 specified number of cycles
1: <script>
2: <!--
3:var myarray = new Array (3);
4:myarray[0] = "Item 0";
5:MYARRAY[1] = "Item 1";
6:MYARRAY[2] = "Item 2";
7:for (i = 0; i < myarray.length; i++) {
8:document.write (myarray + "<br>");
9:}
Ten://-->
One: </script>
22 Setting Future execution
1: <script>
2: <!--
3:function Hello () {
4:window.alert ("Hello");
5:}
6:window.settimeout ("Hello ()", 5000);
7://-->
8: </script>
23 Timed execution function
1: <script>
2: <!--
3:function Hello () {
4:window.alert ("Hello");
5:window.settimeout ("Hello ()", 5000);
6:}
7:window.settimeout ("Hello ()", 5000);
8://-->
9: </script>
24 Cancel timed execution
1: <script>
2: <!--
3:function Hello () {
4:window.alert ("Hello");
5:}
6:var mytimeout = window.settimeout ("Hello ()", 5000);
7:window.cleartimeout (mytimeout);
8://-->
9: </script>
25 Executing functions on page Uninstall
1: <body onunload= "functionname" (); >
2:body of the page
3: </body>
Java script that's what it is. 2: Browser output
26 Accessing the Document object
1: <script language= "java Script" >
2:var Myurl = document. URL;
3:window.alert (Myurl);
4: </script>
27 Dynamic Output HTML
1: <script language= "java Script" >
2:document.write ("<p>here ' s Some information about this document:</p>");
3:document.write ("<ul>");
4:document.write ("<li>referring Document:" + document.referrer + "</li>");
5:document.write ("<li>domain:" + document.domain + "</li>");
6:document.write ("<li>url:" + document.) URL + "</li>");
7:document.write ("</ul>");
8: </script>
28 Output Line Wrapping
1:document.writeln ("<strong>a</strong>");
2:document.writeln ("B");
29 Output Date
1: <script language= "java Script" >
2:var thisdate = new Date ();
3:document.write (Thisdate.tostring ());
4: </script>
30 time zone for the specified date
1: <script language= "java Script" >
2:var Myoffset =-2;
3:var currentdate = new Date ();
4:var Useroffset = Currentdate.gettimezoneoffset ()/60;
5:var timezonedifference = Useroffset-myoffset;
6:currentdate.sethours (currentdate.gethours () + timezonedifference);
7:document.write ("The time and date in the", Europe is: "+ currentdate.tolocalestring ());
8: </script>
31 Set Date output format
1: <script language= "java Script" >
2:var thisdate = new Date ();
3:var thistimestring = thisdate.gethours () + ":" + thisdate.getminutes ();
4:var thisdatestring = thisdate.getfullyear () + "/" + thisdate.getmonth () + "/" + thisdate.getdate ();
5:document.write (thistimestring + "on" + thisdatestring);
6: </script>
32 Reading URL Parameters
1: <script language= "java Script" >
2:var urlparts = document. Url.split ("?");
3:var parameterparts = Urlparts[1].split ("&");
4:for (i = 0; i < parameterparts.length; i++) {
5:var pairparts = parameterparts.split ("=");
6:var pairname = pairparts[0];
7:var pairvalue = pairparts[1];
8:document.write (Pairname + ":" +pairvalue);
9:}
: </script>
Do you still think HTML is stateless?
33 Open a new Document object
1: <script language= "java Script" >
2:function NewDocument () {
3:document.open ();
4:document.write ("<p>this is a New document.</p>");
5:document.close ();
6:}
7: </script>
34 Page Jump
1: <script language= "java Script" >
2:window.location = "http://www.99n9.com/";
3: </script>
35 Add Page Load Progress window
1: 2: 3: <script language= ' java script ' >
4:var placeholder = window.open (' holder.html ', ' placeholder ', ' width=200,height=200 ');
5: </script>
6: <title>the Main page</title>
7: 8: <body); " >open a Full-screen window</a>
65 actions for new windows and parent windows
1: <script language= "java Script" >
2://define New window
3:var NewWindow = window.open ("128a.html", "NewWindow");
4:newwindow.close (); Close a new open window in a parent window
5: </script>
6: Close the parent window in a new window
7:window.opener.close ()
66 writing to the new window
1: <script language= "java Script" >
2:var NewWindow = window.open ("", "NewWindow");
3:newwindow.document.open ();
4:newwindow.document.write ("This is a new window");
5:newwindow.document.close ();
6: </script>
67 load page to frames page
1: <frameset cols= "50%,*" >
2: <frame name= "frame1" src= "/" 135a.html "" >
3: <frame name= "frame2" src= "/" About:blank "" >
4: </frameset>
5: Load the page in the frame2 in frame1
6:parent.frame2.document.location = "135b.html";
68 Sharing scripts between frames pages
If there is a script in the HTML file in the FRAME1
1:function DoAlert () {
2:window.alert ("Frame 1 is loaded");
3:}
So you can call this method in Frame2
1: <body onload= "Parent.frame1.doAlert" (); >
2:this is frame 2.
3: </body>
69 Data public
You can define data items in a frames page so that the data can be shared by pages in multiple frames
1: <script language= "java Script" >
2:var persistentvariable = "This is a persistent value";
3: </script>
4: <frameset cols= "50%,*" >
5: <frame name= "frame1" src= "/" 138a.html "" >
6: <frame name= "frame2" src= "/" 138b.html "" >
7: </frameset>
This allows variables to be used in both frame1 and frame2 persistentvariable
70 Framework code Base
Based on some of the above ideas, we can use a hidden frame page as a code base for the entire frameset
1: <frameset cols= "0,50%,*" >
2: <frame name= "Codeframe" src= "/" 140code.html "" >
3: <frame name= "frame1" src= "/" 140a.html "" >
4: <frame name= "frame2" src= "/" 140b.html "" >
5: </frameset>