JS Rookie Study

Source: Internet
Author: User

( i )

1

The code is as follows:


Writing HTML content to the page
document.write ("


2

The code is as follows:


To prevent JavaScript-enabled browsers from displaying JS as the content of a page
The two forward slash at the end of the comment line is a JavaScript comment symbol that prevents the JavaScript compiler from compiling the line.
<script type= "Text/javascript" >
<!--
document.write ("Hello world!");
-
</script>


3

The code is as follows:


Various ways to do the OnLoad event
The first one joins the OnLoad event via the body tag
<script type= "Text/javascript" >
Function message () {alert ("The cue box is called through the OnLoad event. ");}
</script>
<body onload= "message ()" >
The second calls the OnLoad event directly with the window function
<script type= "Text/javascript" language= "JavaScript" >
Window.onload=message;
Function message () {alert ("The cue box is called through the OnLoad event. "); }
</script>


4
Where JavaScript is placed
When the page is loaded, JavaScript is executed in the body section. (Direct execution)
When called, JavaScript in the head section is executed.
Head section
The script that contains the function is located in the Head section of the document. This allows us to ensure that the script is loaded before the function is called.

5.
The role of semicolons
Semicolons are optional (according to the JavaScript standard), the browser uses the end of the line as the end of the statement, and by using a semicolon, you can write multiple statements in one line.

6.
Rules for JavaScript variable names:
Variables are case sensitive (Y and Y are two different variables)
The variable must start with a letter or an underscore

7.
Declaration of a variable
If the variable you are assigning has not been declared, the variable is automatically declared.
Cases:
x=5; Carname= "Volvo";
The effect of these statements is the same as the following: Var x=5; var carname= "Volvo";

8.
Comparison operators
Operator Description Examples
= = = Congruent (value and type) x===5 is true; x=== "5" is false

9.
Conditional operator (trinocular operator)
JavaScript also includes conditional operators that assign values to variables based on certain conditions.
Name= ("Liuhuan" = "LH")? " Liu Huan ":" Singer ";


10.
Get current system time (hours)
var d = new Date ()
var time = d.gethours ()

11.
Random number
var num=math.random ();
The resulting pseudo-random number is between 0 and 1 (with 0, without 1), that is, the return value may be 0, but is always less than 1. Random numbers when JScript is loaded for the first time

Automatic generation of the generator.

12.
Get today's Day of the week (Sunday is 0, week 1-6 is 1-6)
var d = new Date ()
Theday=d.getday ()

13.
Trigger events for buttons
<input type= "button" onclick= "Disp_alert ()" Value= "Show Warning Box"/>

14.
Popup Box Content Wrapping
Alert ("Say Hello again to you!") Here, we show you how to add a line to a warning box by "+ \ n ' +". ")

15.
Confirmation Box (Delete method)
Confirm ("text")
<script type= "Text/javascript" >
function Show_confirm ()
{
var r=confirm ("Confirm delete?");
if (r==true) {
Alert ("Delete succeeded!");
}
else{
Alert ("Delete failed!");
}
}
</script>

16.
Popup box for user interaction (you can enter a hint box for text)
Prompt ("text", "Default value")
<script type= "Text/javascript" >
function Disp_prompt ()
{
var name=prompt ("Please enter your name", "Bill Gates")
if (name!=null && name!= "") {
document.write ("Hello! "+ name +" How was your day? ")
}
}
</script>

17.
Functions with parameters and return values
<script type= "Text/javascript" >
function product (A, B)
{
return a*b;
}
</script>
<body>
<script type= "Text/javascript" >
document.write (Product (6,5))
</script>
</body>

18.
For loop (the H tag in the HTML is dynamically generated in this example)
<body>
<script type= "Text/javascript" >
for (i = 1; I <= 6; i++) {
document.write ("document.write ("}
</script>
</body>

19.
Break Jump Statement
<script type= "Text/javascript" >
var i=0
for (i=0;i<=10;i++) {
if (i==3) {break}
document.write ("number is" + i)
document.write ("<br/>")
}
</script>
<p> Explanation: The loop will break at i=3. </p>


20.
Continue jump Statement
<script type= "Text/javascript" >
var i=0
for (i=0;i<=10;i++) {
if (i==3) {continue}
document.write ("number is" + i)
document.write ("<br/>")
}
</script>
<p> Explanation: When i=3, the loop is interrupted and the next value starts to loop. </p>
Value is: 01245678910

21st.
For In loop (equivalent to a foreach loop in. net)
<body>
<script type= "Text/javascript" >
var x
var mycars = new Array ()
Mycars[0] = "BMW"
MYCARS[1] = "Mercedes"
MYCARS[2] = "Bentley"

for (x in Mycars)
{
document.write ("The value of x is" +x+ "<br/>");
document.write (Mycars[x] + "<br/>")
}
</script>
</body>


22.
JavaScript events
OnLoad a page or image is finished loading//page loading
OnUnload User Exit Page

onfocus Element gets focus
onblur element loses focus//form validation
onchange user changes the contents of a domain
OnReset reset button is clicked
The onsubmit submit button is clicked//used to validate all form fields before submitting the form.
For example:
(The Checkform () function is called when the user clicks on the confirmation button in the form.) The return value of the Checkform () function is of type bool, and if the return value is true, the

Submit the form, and cancel the submission instead. )
<form method= "POST" action= "xxx.htm" onsubmit= "return Checkform ()" >

OnKeyDown Key of a keyboard is pressed
onkeypress Key of a keyboard is pressed or pressed//keyboard operated
OnKeyUp Key of a keyboard is released

OnClick mouse click on an object
OnDblClick Mouse Double-click an object
OnMouseDown a mouse button is pressed//mouse action
OnMouseMove Mouse is moved
onMouseOut mouse away from an element
onMouseOver Mouse is moved to an element
OnMouseUp a mouse button is released

Onabort image load is interrupted
OnError An error occurred while loading a document or image

OnResize window or frame is resized
Onselect Text is selected

23.
Error hints in JS Err.Description and their try...catch statements
For example:
<script type= "Text/javascript" >
var txt= ""
Function message () {
try{
Adddlert ("Welcome guest!")
}
catch (Err) {
Txt= "There are errors in this page. \ n "
txt+= "Error Description:" + Err.Description + "\ n"
txt+= "click" OK "to continue. \ n "
alert (TXT);
}
}
</script>

24.
Try...catch Statement with confirmation box
<script type= "Text/javascript" >
var txt= ""
Function message () {
try{
Adddlert ("Welcome guest!")
}
catch (Err) {
Txt= "There are errors in this page. \ n "
txt+= "click" OK "to continue viewing this page \ n"
txt+= "click" Cancel "to return to the first page. \ n "
if (!confirm (TXT))
{
Document.location.href= ". /index.html "
}
}
}
</script>
<body>
<input type= "button" value= "view message" onclick= "message ()"/>
</body>


25.
Create a exception (exception or error). (Used with try...catch statements)
For example:
<script type= "Text/javascript" >
var x=prompt ("Please enter a number between 0 and 10:", "")
try{
if (x>10)
Throw "ERR1"
else if (x<0)
Throw "ERR2"
else if (IsNaN (x))
Throw "Err3"
}
catch (er) {
if (er== "ERR1")
Alert ("Error! The value is too large! ")
if (er = = "ERR2")
Alert ("Error! The value is too small! ")
if (er = = "ERR3")
Alert ("Error! This value is not a number! ")
}
</script>


26.
The use of return true and return True
(It can return an argument of type bool and continue to be used for judgment)
function Jiance (msg,url,l) {
Alert ("Are you sure?")
return True
}
function Jieguo () {
if (Jiance ()) {
Alert ("Yes");
}
else{
Alert ("No");
}
}

27.
OnError Events
<script type= "Text/javascript" >
Triggering the OnError event when an error occurs
Onerror=handleerr;
var txt= ""
function Handleerr (msg,url,l) {
Txt= "There are errors in this page. \ n "
txt+= "Error:" + msg + "\ n"
txt+= "url:" + URL + "\ n"
txt+= "line:" + L + "\ n"
txt+= "click" OK "to continue. \ n "
Alert (TXT)
return True
}
Function message () {
Adddlert ("OK?")
}
</script>
<input type= "button" value= "view message" onclick= "message ()"/>
</body>


28.
A backslash is used in JavaScript to add special characters to a text string.
For example:
var txt= "We is the so-called \" Vikings\ "from the north."
document.write (TXT)

29.
JavaScript considerations
1. JavaScript is case sensitive
2. JavaScript ignores extra spaces
3. You can use backslashes for line breaks when writing code
Cases:
document.write ("Hello \

World! ");

( II)

1.
Using the properties of a variable
<script type= "Text/javascript" >
var txt= "Hello world!"
document.write (Txt.length)
</script>


2.
Converts all letters in a string to uppercase letters.
<script type= "Text/javascript" >
var str= "Hello world!"
document.write (Str.touppercase ())
</script>


3.
JS in a variable to add a hyperlink
<script type= "Text/javascript" >
var txt= "Hello world!"
document.write ("<p> hyperlink:" + Txt.link ("http://www.w3school.com.cn") + "</p>")
</script>

4.
The IndexOf method, where the first occurrence of a specified character in a string is positioned. If no return-1 is found, it is case-sensitive)
<script type= "Text/javascript" >
var str= "Hello world!"
document.write (Str.indexof ("Hello") + "<br/>")//1
document.write (Str.indexof ("World") + "<br/>")//-1
document.write (Str.indexof ("World"))//6
</script>

5.
Match () method
Use Match () to find a specific character in the string and, if found, to return the character.
<script type= "Text/javascript" >
var str= "Hello world!"
document.write (Str.match ("World") + "<br/>")//world
document.write (Str.match ("World") + "<br/>")//null
document.write (Str.match ("worlld") + "<br/>")//null
document.write (Str.match ("world!"))//world!
</script>


6.
The replace () method replaces some character with some characters in a string.
var str= "Visit microsoft!"
document.write (Str.replace ("Microsoft", "W3school"))


7.
Merging two arrays
<script type= "Text/javascript" >
var arr = new Array (3)
Arr[0] = "George"
ARR[1] = "John"
ARR[2] = "Thomas"
var arr2 = new Array (3)
Arr2[0] = "James"
ARR2[1] = "Adrew"
ARR2[2] = "Martin"
document.write (Arr.concat (ARR2))
</script>

( three )

1.js Regular Expression (RegExp object)
The RegExp object has 3 methods: Test (), exec (), and compile ().
①test () method
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");
②exec () method
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");

JS Rookie Study

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.