JavaScript Beginner Learning

Source: Internet
Author: User
Tags getdate

Programming, programmers not only have to master a language, but to learn a few more doors.

The source of this study is from Lemon College http://www.bjlemon.com/, hereby declares.

First Lesson
main features of 1:javascript
interpreted: does not need to compile, the browser directly interprets the execution
Object based: We can directly use the object JS has created
Event-driven: can input the client in an event-driven manner without having to go through the server-side program
Security: Access to local hard disks is not allowed and data cannot be written to the server
Cross-platform: JS relies on the browser itself, regardless of the operating system

Lesson Two
How to write JavaScript in a Web page
1: Embed JavaScript directly in the page
<script language= "JavaScript" >
    
JavaScript Programs
</script>
JavaScript can be inserted in the middle of the
can also be placed in the middle of the <body></body> label
most commonly placed between

The following example inserts the JavaScript code in the middle of the

var now=New Date (); // gets an instance of the Date object var hour=now.gethours (); // gets the number of hours var min=now.getminutes (); // gets the number of minutes Alert ("Current Time" +hour+ ":" +min+ "\ n Welcome to Lemon College http://www.bjlemon.com/") ; </script>

The results of the case run as follows

  

Case 2 code is as follows

varnow=NewDate ();//Get Date Object        varYear=now.getyear (+1900);//get year, in JS the year needs to add 1900 to display the year at this moment        varMonth=now.getmonth (+1);//get the month, the month is 0-11, so you need to add 1 in JS        varDate=now.getdate ();//Get the day        varDay=now.getday ();//get the day of the week        varday_week=NewArray ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); varweek=Day_week[day]; varTime= "Current time:" +year+ "year" +month+ "month" +date+ "Day" +Week;    alert (time); </script>

Case 2 operation results are as follows

2: Reference to external JavaScript

If the script is more complex or the same piece of code is used by many pages, you can place the script code in a separate file with a. js extension, and then you need to link the JavaScript file in a Web page that uses that code to

<script language= "javascript" src= "*.js" >
</script>
(recommended) The above code is generally written in the middle of better

in the. js suffix file, you do not need to use the <script></script> tag pair to enclose

<body onload= "getDate ()" > represents the method that calls the GetDate () method when the page is loaded () getDate () is defined in the. js suffix file



this case suffix is. html

This case suffix is. js

functiongetdate () {varnow=NewDate ();//Get Date Object        varYear=now.getyear (+1900);//get year, in JS the year needs to add 1900 to display the year at this moment        varMonth=now.getmonth (+1);//get the month, the month is 0-11, so you need to add 1 in JS        varDate=now.getdate ();//Get the day        varDay=now.getday ();//get the day of the week        varday_week=NewArray ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); varweek=Day_week[day]; varTime= "Current time:" +year+ "year" +month+ "month" +date+ "Day" +Week;    alert (time); }

Combined with the results of the above two cases,

Lesson three
The syntax of JavaScript
The syntax of 1:javascript
1.1:JS variables are case-sensitive
Usename,usename This is a two different variable.
1.2: The semicolon at the end of each line is optional, if the end of the statement is not a semicolon, then JS
Will automatically end this line of code as the end of the statement
alert ("Hello World");
alert ("Hello World")
1.3: variable is weak type
use the var operator only when defining variables
For example: Var usename= "Biexiansheng";
var age=22;
1.4: Use curly braces tag code block
{//code} The statements enclosed in curly braces are executed in sequence
1.5: Notes
1.5.1: Single-line comment//
single-line comments begin with a double slash "//" and the text after "//" is the comment content
The contents of the comment do not work during code execution.
var now=new date ();//Get Date Object
1.5.2: Multiline Comment/**/
Multiline comments begin with/*, end with */, the contents of which are commented content
does not play any role during code execution.
            /*
* Function: Get the current date
*biexiansheng
            */
function Getclock () {
//Content
            }

JavaScript Beginner Learning

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.