Getting Started with JavaScript

Source: Internet
Author: User

First, JavaScript Foundation

 1, the concept: JavaScript abbreviation JS, is running in the browser side of a script language, code does not need to compile, can run directly, and read into a line, run a line, more for: browser-side verification, Ajax, rich clients, etc.

2, write the location:

Inside the tag: write the JS event code inside the tag

Inside the page: in the <script> </script> tag internal write JS code

In the <body></body>

In the

External. JS file:<script type= "Text/javascript" src= "script.js" ></script>

3. JavaScript Comments:

Single-line Comment://

Multiline Comment:/* */

4. Mouse events:

OnClick: Mouse Click event to execute script when mouse click

OnDblClick: Mouse Double-click event, double-click Element Execution Script

onMouseOver: Mouse over event, execute script when mouse moves on element

onMouseOut: Executes the script when the mouse moves out of the element

OnMouseUp: Execute script when mouse button is released

OnMouseDown: Executes the script when the mouse button is pressed

OnMouseMove: Execute script when mouse pointer moves

code example:

1 <!DOCTYPE HTML>2 <HTML>3     <Head>4         <MetaCharSet= "UTF-8">5         <title>Insert Title here</title>6         <Scripttype= "Text/javascript"src= "Script.js"></Script>//External JS7         <Scripttype= "Text/javascript">//8 window.onload=function() {//= called when page load is complete9                 varbtn=document.getElementById ("Btn3");Ten Btn.onclick=function(){ One Alert ("Point 3"); A                 } -             } -         </Script> the     </Head> -     <Body> -         <ButtonID= "BTN1"onclick= "alert (' point 1 ')">Point 1</Button>//tags inside js -         <ButtonID= "BTN2">Point 2</Button> +         <ButtonID= "Btn3">Point 3</Button> -         <ButtonID= "Btn4">Point 4</Button> +         <Scripttype= "Text/javascript">//<body></body> inside JS A             varbtn=document.getElementById ("btn2"); at Btn.onmouseover=function(){ - Alert ("page: Point 2! "); -             } -         </Script> -     </Body> - </HTML>

Code in Script.js:

1 window.onload=function() {2     var btn=document.getelementbyid (" Btn4 "); 3     btn.onclick=function() {4         alert ("External: Why do you order me! "); 5     }; 6 };

Where the button "point 3" of the JS code will not be executed, because Window.onload will only be called once, first called the external JS in the Window.onload

5. Variables:

Disclaimer: JS is a weakly typed language, you do not need to specify the type of variable when declaring a variable, you only need to use the var keyword

Assignment: JS is a dynamic type of language, you can assign any type of value to a variable, you can change the type of the variable in the process of use

6. Functions

In JS, the function is also an object, you can assign a function reference to a variable

JS in the function does not check the type and number of parameters, so there is no overload this thing

Functions: including definition of functions, invocation of functions

Declaration of function: function declaration by using Functions keyword

Anonymous functions:

var a=function () {//anonymous function
Alert ("function a");
}

Named functions:         

function sum (A, b) {
var sum=a+b;
return sum;
}

Invocation of function: reference to function + ();

A ();

sum (2,3);

Function of return: 1, execution of termination function; 2. Return the result of the function to the caller

7. Objects

There are two ways of creating objects in JS:

1, var obj=new Object ();

2, Var obj={};

To add properties and methods dynamically to an object:

Getting Started with JavaScript

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.