HTML Basics of JS

Source: Internet
Author: User
Tags script tag

Ext.: http://www.imdsx.cn/index.php/2017/07/27/html4/

The HTML of the three sharp-edged JS is also called JavaScript, look like a little connection with Java, in fact, he and Java have no relationship with half a dime, JavaScript and we learn Python, Go, Java, C + +, are a separate language, The Python interpreter has Python2.7, python3.x, and the browser has the function of explaining JavaScript, so it is one of the three tools of HTML.

In HTML, you can write Javascript/js code in head, wrapped by a script tag, and when the browser interprets the HTML, when it encounters a style tag, it is interpreted according to the CSS rules, and when it encounters a script tag, it is interpreted in accordance with the syntax rules of JavaScript.

Introducing JavaScript code, similar to the import of Python

<script src= "Public.js" type= "Text/javascript" ></script>

Introduction of JS in Head and the introduction of JS difference in body

HTML code from the top and bottom, if the introduction of JS in the head, affecting the speed of the page open, there is a risk, so usually placed at the bottom of the HTMLBody, so that the content of the page first displayed, and finally loading JS. Note: Write at the bottom to have a baseline, written in the body inside the bottom.

Comments

Single-line comment via//Multiline pass/* */

JS variable

      function func () {var//} 

JS basic data type (JavaScript declaration data type via new)

String

//Defining Stringsvarstr = ' You are happy! ‘; varname = ' Big bro '; //concatenation of StringsvarName_str = name+str; //string Manipulationstr = ' big bro 'Str.charat (0gets a character in a string according to the corner Mark Char character str.substring (1,3gets a string subsequence greater than or equal to x less than Y str.length according to the angle tag gets the string length Str.concat (' Professor Niu ') Stitching string Str.indexof (Master) Gets the position of the subsequence Str.slice (0,1) slice start end str.tolowercase () change to lowercase str.touppercase () change uppercase Str.split (' Division ', 1The slice returns the array parameter 2 for the first X element number type of the array after the split (JavaScript has only one numeric type.) Numbers can be with decimal points or without)varAge = 18;varScore = 89.22; number= ' 18 '; //String gotovarn =parseint (number);//Convert to decimalf =parsefloat (number) Boolean type (trueOrfalse)vart =true;varf =false;

Array type (which is the list of Python)

//The first method of creating var list = new Array ();List[0] = ' big bro '; list[1] = ' Professor Niu '; //second method of creationvarList2 =NewArray (' Big bro ', ' Professor Niu '); //The third method of creationvarList3 = [' Big bro ', ' Professor Niu ']; Array ManipulationvarList3 = [' Big bro ', ' Professor Niu ']; List3.length the length of the array List3.push (' Dsx 'tail Chase ah parameter list3.shift () the head gets an element and removes the element List3.pop () tail gets an element and deletes the element List3.unshift (' Dsx 'the head inserts a data list3.splice (start, DeleteCount, value) to insert, delete, or replace an array of elements List3.splice (n,0, Val) specifies the position of the insertion element List3.splice (n,1, Val) specifies the position of the replacement element List3.splice (N,1) to specify the location to delete the element List3.slice (The) slices; List3.reverse () Invert list3.join (‘-‘) to stitch the array into a string List3.concat ([' ABC ']) array and array stitching list3.sort () sort

Object type (equivalent to a Python dictionary)

var dict = {name: ' Dsx ', age:18,sex: ' Male ' }; var age =var name = dict[' name '];  Delete dict[' name '] deletes  deleted dict.age Delete

Timer

// parameter 1 is the function performed by the timer, and the second parameter is the time interval of the timer to work in milliseconds function T1 () {    console.log(' I am big bro ')}setinterval (' T1 () ', 5000); // can run methods

JS Conditional Judgment Statement

if(condition) {Execute code block}Else if(condition) {Execute code block}Else{execute code block};if(1 = = 1) {Console.log ()}Else if(1! = 1) {Console.log ()}Else if(1 = = 1) {Console.log ()}Else if(1!== 1) {Console.log ()}Else if(1 = = 1 && 2 = = 2) {// andconsole.log ()}Else if(1 = = 1 | | 2 = = 2) {//orconsole.log ()}Switch(a) { Case1: Console.log (111);  Break;  Case2: Console.log (222);  Break; default: Console.log (333)}

JS Loop statement

= [' BMW ', ' Mercedes ', ' Nissan '= ' bmw Benz Nissan '= ' bmw ': ' BMW ', ' Mercedes ': ' BC '};  for (var in tmp) {    console.log (Tmp[i])} The second loop does not support the dictionary's Loop for (var i = 0; I  < 10; i++) {    console.log (Tmp[i])} third loop while (1 = = 1) {    console.log (  111)} 

function definition

1, common functionsfunctionfunction name (formal parameter, formal parameter, formal parameter) {EXECUTE code block} function name (formal parameter, formal parameter, formal parameter);2anonymous function anonymous function does not have a name, cannot be called when found, the entire function as a parameter passed setinterval (function() {Console.log (11)}, 5000);3, self-executing function creation function and automatic execution when multiple JS files are introduced, the function name may be duplicated, and the self-executing function ensures that each JS file is parsed to generate a separate container to prevent the calling conflict (function(name) {Console.log (name)}) (' Dsx '); Scope python scopes are scoped by functions, and other languages are scoped with code blocks ({}). JavaScript is scoped as a functionfunctiontmp () {varname = ' DSX '; Console.log (name)}tmp (); Console.log (name);2, the function scope has been created before the function is called.varname = ' Nhy ';functionA () {varName= ' DSX '; functionB () {Console.log (name); }    returnB}varc =a (); C ();3, the scope of the function exists in the scope chain (when the code does not execute, Mr. into the scope chain) when function nesting function, each function is a scope, multi-layer is called scope chain, find follow scope chain rulefunctionouter () {name= ' nn '; functioninner () {varname = ' II 'Console.log (' In ', Name)} Console.log (' Out ', name); Inner ()}outer (); When the function is not called, only the scope is generated, and when the call follows the scope chain execution, name is HHHfunctionouter () {varname = ' NN '; functioninner () {Console.log (' In ', Name)} varname = ' HHH '; Console.log (' Out ', name); Inner ()}outer ();4, within a function, the local variable declares JavaScript's function before running to find all local variables within the function to executevarxxx;functionfunc () {console.log (name); varname = ' DSX ';} Func ();

Object oriented

//in JavaScript, methods are similar to classes in that they differ in whether they have this, and if so, they can be used as classes.//The JavaScript class has the Python constructor by default __init__functionFoo (name) { This. Name =name;}//JavaScript needs to use the New keyword to create objects when creating objectsvarobj =NewFoo (' Dsx '); Console.log (Obj.name); //A method is defined in a class, although it can be used, but there are duplicate instance methods in multiple instances, wasting resources//when creating an object, say creates a say method each time the object is created. functionFoo1 (name) { This. Name =name;  This. Say =function() {Console.log ( This. Name)}}varObj1 =NewFoo1 (' Dsx_obj1 '); Obj1.say ();//refine the definition of a classfunctionFoo2 (name) { This. Name =name}//class of the prototype, the common method is extracted, when the instantiation, only created an object called Foo2, the object is only name, in the call method to the current Foo in the search, not found, will be in the prototype to findwhether the method is available. There is execution, there is no error foo2.prototype={say:function() {Console.log ( This. Name)}};varObj2 =NewFoo2 (' Dsx_obj2 '); Obj2.say ();

Serialization of

Json.stringify (obj) serialization of Json.parse (str) deserialization

Escape

Escape Chinese or special characters

1, in the standard URL of the specification is not allowed to appear in Chinese characters or some special characters, so to be escaped 2, & represents the link of the parameter, if you want to pass & to the backend then must escape decodeURI (URL) Escaped characters in the URL in the decodeuricomponent (URL) URI component of the escape character in the encodeURI (URL) Uri escaped from the character string escaped in the URI component encodeURIComponent (URL) escape  var name= ' big bro ' escape (name) to string escape unescape (name) escape string decoding

HTML Basics of JS

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.