Clean up the good JavaScript methods and techniques 1th/3 Page _ Basics

Source: Internet
Author: User
Tags time and date
Fit to read: Know nothing about JavaScript ~ one step away from mastery
Basics: HTML


That's what JavaScript is about. 1: basic knowledge

1 Creating script blocks

1: <script language= "JavaScript" >
2:javascript Code goes here
3: </script>



2 Hide Script code

1: <script language= "JavaScript" >
2: <!--
3:document.write ("Hello");
4://-->
5: </script>


Do not execute related code in browsers that do not support JavaScript

3 when the browser does not support the display

1: <noscript>
2:hello to the Non-javascript browser.
3: </noscript>



4 Linking external script files

1: <script language= "JavaScript" 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= "JavaScript" >
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= "JavaScript" >
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= "JavaScript" >
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= "JavaScript" >
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= "JavaScript" >
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= "JavaScript" >
2: <!--
3:window.alert ("Hello");
4://-->
5: </script>



16 Pop-up Confirmation box

1: <script language= "JavaScript" >
2: <!--
3:var result = Window.confirm ("Click OK to continue");
4://-->
5: </script>



17 Defining functions

1: <script language= "JavaScript" >
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= "/" Javascript: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[i] + "<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>

That's what JavaScript is about. 2: Browser output


26 Accessing the Document object

1: <script language= "JavaScript" >
2:var Myurl = document. URL;
3:window.alert (Myurl);
4: </script>



27 Dynamic Output HTML

1: <script language= "JavaScript" >
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= "JavaScript" >
2:var thisdate = new Date ();
3:document.write (Thisdate.tostring ());
4: </script>



30 time zone for the specified date

1: <script language= "JavaScript" >
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>

Current 1/3 page 123 Next read the full text
Related Article

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.