JavaScript knowledge points

Source: Internet
Author: User
Tags getdate logical operators setcookie

Output
<script>
document.write ("document.write ("<p>this is a paragraph</p>");
</script>

Statement
document.getElementById ("Demo"). Innerhtml= "Hello World";

JavaScript has a dynamic type
<script>
var person={
FirstName: "Bill,"
LastName: "Gates",
id:5566
};
document.write (person.lastname + "<br/>");
document.write (person["id"] + "<br/>");
</script>

JavaScript objects
<script>
Person=new Object ();
Person.firstname= "Bill";
Person.lastname= "Gates";
person.age=56;
Person.eyecolor= "Blue";
document.write (Person.firstname + "is" + Person.age + "Years");
</script>

Function
function MyFunction ()
{
Alert ("Hello world!");
}
<button onclick= "myFunction ()" > click here </button>

JavaScript operators
Count
X=y+2
X=y-2
X=y*2
X=y/2
x=y%2
X=++y
Assign value
X=y
X+=y
X-=y
X*=y
X/=y
X%=y
Comparison operators
X==8 to False
X===5 is true;x=== "5" is false
X!=8 is True
X>8 to False
X<8 is True
X>=8 to False
X<=8 is True
logical operators
(x < ten && y > 1) True
(x==5 | | y==5) is false
! (x==y) is true
Conditional operators
Greeting= (visitor== "PRES")? " Dear president ":" Dear ";

Judgment statement
If statement-use this statement to execute code only if the specified condition is true
If...else Statement-executes code when the condition is true and executes other code when the condition is false
If...else If....else Statement-Use this statement to select one of several code blocks to execute
Switch statement-Use this statement to select one of several code blocks to execute

Looping statements
for (Var i=0;i<cars.length;i++)
{
document.write (Cars[i] + "<br>");
}
For-loop code block for a certain number of times
For/in-Looping through the properties of an object
While-loops the specified block of code when the specified condition is true
Do/while-also loops the specified block of code when the specified condition is true
The break statement can be used to jump out of a loop. With a label reference, the break statement can be used to jump out of any JAVASCRIPT code block:
Continue iteration in a statement interrupt loop

JavaScript errors-Throw, Try, and Catch
The TRY statement tests the code block for errors.
The catch statement handles the error.
The throw statement creates a custom error.
Try
{
Adddlert ("Welcome guest!");
}
catch (Err)
{
Alert ("Error");
}
}
Throw statement
Throw creates or throws an exception (exception).
var X=document.getelementbyid ("Demo"). Value;
if (x== "") throw "empty";
if (IsNaN (x)) throw "not a number";

JavaScript HTML DOM

JavaScript HTML DOM Elements (nodes)
Add and remove nodes (HTML elements).
<div id= "Div1" >
<p id= "P1" > This is a paragraph. </p>
<p id= "P2" > this is another paragraph. </p>
</div>
<script>
var para=document.createelement ("P");
var Node=document.createtextnode ("This is the new paragraph. 23423 ");
Para.appendchild (node);
var Element=document.getelementbyid ("Div1");
Element.appendchild (para);
</script>

Delete an existing HTML element
<div id= "Div1" >
<p id= "P1" > This is a paragraph. </p>
<p id= "P2" > this is another paragraph. </p>
</div>
<script>
var Parent=document.getelementbyid ("Div1");
var Child=document.getelementbyid ("P1");
Parent.removechild (child);
</script>

REGEXP is a regular expression reference more full http://www.w3school.com.cn/jsref/jsref_obj_regexp.asp
When you retrieve a text, you can use a pattern to describe what you want to retrieve. REGEXP is this pattern.
The RegExp object has 3 methods: Test (), exec (), and compile ().

The test () method retrieves the specified value in the string. The return value is true or false.
var patt1=new RegExp ("E");
document.write (Patt1.test ("The best things in life is free");

The EXEC () method retrieves the specified value in the string. The return value is the value that was found. If no match is found, NULL is returned.
var patt1=new RegExp ("E");
document.write (Patt1.exec ("The best things in life is free");
var patt1=new RegExp ("E", "G");//When using the "G" parameter, find the first "E" and store its location if you run exec () again, retrieve it from the stored location and find the next "E" and store its location

The compile () method is used to change the REGEXP.
var patt1=new RegExp ("E");
document.write (Patt1.test ("The best things in life is free");
Patt1.compile ("D");
document.write (Patt1.test ("The best things in life is free");

Window size
<script>
var w=window.innerwidth
|| Document.documentElement.clientWidth
|| Document.body.clientWidth;

var h=window.innerheight
|| Document.documentElement.clientHeight
|| Document.body.clientHeight;

X=document.getelementbyid ("demo");
X.innerhtml= "Browser's internal window width:" + w + ", Height:" + H + ". "
</script>

Window screen available height available width
<script>
document.write ("Usable width:" + screen.availwidth);
document.write ("Available height:" + screen.availheight);
</script>

Window Location
document.write (LOCATION.HREF);
document.write (Location.pathname);
Window.location.assign ("http://www.w3school.com.cn")
Location.hostname returns the domain name of the web host
Location.pathname returns the path and file name of the current page
Location.port returns the port of the web host (80 or 443)
Location.protocol returns the Web protocol used (HTTP///https://)


Window history
History.back ()-Same as clicking Back button in browser
History.forward ()-Same as clicking the button in the browser forward

Warning box, confirmation box, prompt box.
Alert ("Text")
Confirm ("text")
Prompt ("text", "Default value")

JavaScript Timings
var t=settimeout ("Alert (' 5 seconds! ')", 5000)
Cleartimeout (settimeout_variable)

Create and store cookies
function Setcookie (c_name,value,expiredays)
{
var exdate=new Date ()
Exdate.setdate (Exdate.getdate () +expiredays)
document.cookie=c_name+ "=" +escape (value) +
((expiredays==null)? "": "; expires=" +exdate.togmtstring ())
}

Check if cookies are set
<script type= "Text/javascript" >
function GetCookie (c_name)
{
if (document.cookie.length>0)
{
C_start=document.cookie.indexof (c_name + "=")
if (c_start!=-1)
{
C_start=c_start + c_name.length+1
C_end=document.cookie.indexof (";", C_start)
if (c_end==-1) c_end=document.cookie.length
Return unescape (document.cookie.substring (c_start,c_end))
}
}
Return ""
}

function Setcookie (c_name,value,expiredays)
{
var exdate=new Date ()
Exdate.setdate (Exdate.getdate () +expiredays)
document.cookie=c_name+ "=" +escape (value) +
((expiredays==null)? "": "; expires=" +exdate.togmtstring ())
}

function Checkcookie ()
{
Username=getcookie (' username ')
if (username!=null && username!= "")
{alert (' Welcome again ' +username+ '! ')}
Else
{
Username=prompt (' Please enter your name: ', ' ")
if (username!=null && username!= "")
{
Setcookie (' username ', username,365)
}
}
}
</script>

JAVASCRIPT Frames >>

JavaScript knowledge points

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.