HTML Basics (JavaScript)

Source: Internet
Author: User

A two-point description of the JavaScript concept:

1, it is a network scripting language, used to embed in HTML pages for Web development purposes.

2. It has nothing to do with Java's half-dime relationship.


How to use

1. How to embed in HTML

1-1, the script is written in the <body> tag in the bottom, that is, </body> above.

1-2, defined directly in the page: <script type= ' text/javascript ' > Script code </script>

1-3, as a file is loaded: <script src= "js file path" ></script>

1-4. Writing specification:

1-4-1, add a semicolon at the end of each executable statement (;)

1-4-2, note indenting and adding spaces to improve code readability

1-4-3, single-line comment//Multiline comment with/* content */


2. Variables

The scope of a JavaScript variable is broken down by a function block (that is, by a pair of curly braces {}), for, if, and so on, which is not a scoping criterion for scopes:

Variables outside of function, whether global or local, are in effect in all the function of the current page

Variables within function, local variables are only valid in the current function. Global variables are global in effect.

2-1. Global variable: name = ' value ';

2-2, local variables: var name = ' value ';


3. String method

>var name = ' value ' #定义一个字符串 with one space on each side

>name.trim () #去除字符串两边的空格

"Value"

>name.charat (1) #按照索引1获取字符串的第2个值 (index 0 is the 1th value)

"V"

>name.substring (1,3) #按照索引范围获取一个子序列字符串

"VA"

>name.indexof (' u ') #查找字符串中字符的索引值

4

>name.length #获取字符串长度

7


4. Arrays (list)

>var list = [] or var list = Array () #声明数组

#添加

>list.push (1) #追加数字1

>list.push (' abc ')

[1, "ABC"]

>list.unshift (0) #在最前面插入

[0, 1, "ABC"]

>list.splice (2,0, ' def ') #在索引值2前面插入

[0, 1, "Def", "ABC"]

#获取 (remove)

>list.pop () #获取最后一个值

"ABC"

>list

[0, 1, "Def"]

>list.shift () #获取第一个值

0

>list

[1, "Def"]

List.splice (index,n) #获取从index处开始的N个元素

>list.splice (a)

["Def"]

>list

[1]


#其他

>list=[1, 2, 3]

>list.slice (0,2) #切片

[1, 2]

>list2=[3,4,5]

>list3=list.concat (LIST2) #合并

[1, 2, 3, 3, 4, 5]

>list3.reverse () #翻转

[5, 4, 3, 3, 2, 1]

>list3.join ('_') #字符串化

"5_4_3_3_2_1"

>list3.length #长度

6


5. Functions (function)

Functions are divided into:

5-1. Basic functions: Functions that need to be called through an event to execute

Example:

<script type= "Text/javascript" > Function Foo (ARG) {console.log (ARG); } </script>

5-2. Self-executing function: function that executes automatically when the page frame is loaded

Example:

<script type= "Text/javascript" > (function Foo (ARG) {console.log (ARG); }) (' Test ') </script>


6. Circulation

The loop is divided into:

6-1, for: Loop code block a certain number of times

Example:

for (var i=0; i<5; i++) {x = ' the number is ' + i;            Console.log (x); }

6-2. For/in: Looping through the properties of an object

Example:

var list = [1,2,3,4] for (var i in list) {x = "the number is" + list[i];            Console.log (x); }

6-3, while: loops the specified block of code when the specified condition is true

Example:

var list = [1,2,3,4] while (list.length>1) {List.pop () Console.log (list); }

6-4, Do/while: First execute the code once, if the condition is true, it will repeat the loop

Example:

var list = [1,2,3,4];                do{List.pop ();            Console.log (list); }while (list.length>1);


7. Exception Handling

Grammar:

try{

Run the code here

}catch (Err) {

Handle the error here

}finally{

Non-mandatory, if any, executes this block of code, whether or not it is an error

}

Example:

try{var n = m;        }catch (Err) {Console.log (err);        }finally{Console.log (' Code is End '); }

part of the blog content and ideas organized from Wu Jianzi blog .

This article from "A rookie on the Sky" blog, please be sure to keep this source http://rmeos.blog.51cto.com/761575/1743900

HTML Basics (JavaScript)

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.