JavaScript Basics (2)

Source: Internet
Author: User

Javascript:
One: Data type
1. Numeric type: For example 3 5 1.2 3.4
2. Boolean: Two values True or FALSE, such as true or false
3. String: For example: ' I am a jelly doughnut ' consists of one or more characters,
A series of characters (which can also be called a string object) enclosed in single or double quotation marks
4. Null value: denoted by the keyword NULL. If the variable is declared but not assigned, this type
5. Undefined: denoted by the keyword undefined, if the variable is not declared it is undefined type
6. Array: The data is declared with the new Array () (also known as an array object)
7. Object: {} An object is declared with a pair of curly braces
Two:
parseint (): Converts a string to an integer
Parsefloat (): Converts a string to a floating-point number
IsNaN (): Determines whether the given value is a numeric value, returns True if it is not a value, returns false
Three: Events
1> Mouse Events
Onclick: A mouse click event that fires when the mouse clicks
Onmouseover: Mouse over an event that is triggered when the mouse moves over an element
onMouseOut: Mouse out event triggers when the mouse moves out of an element
2> keyboard Events
Onkeyup: Keyboard Lift event, this event triggers when the keyboard is lifted
Onkeydown: Keyboard pressed event, this event triggers when the keyboard is pressed
onMouseOut: Mouse out event triggers when the mouse moves out of an element
3> Page Events
Onload: Page Load event, this event triggers when this page is opened
Onunload: Page Unload event, this event triggers when the page is closed
4> Form Events
Onblur: Loses focus event, this event fires when the cursor leaves the text box
Onsubmit: Form submission event, this event triggers when the Submit button is clicked
Onchange: Content Change event, which triggers when content is changed
Four: Mastering Math objects
Math.Round (): Rounding For example: alert (Math.Round (1.2)) The result is 1
Math.ceil (): Rounding up for example: alert (Math.Round (1.2)) The result is 2.
Math.floor (): Rounding down For example: alert (Math.Round (1.2)) The result is 1.
Math.random (): Generates a random number between 0---1
Alert (parseint (Math.random () *10) generates a random number between 1 and 10
Five: The flexible use of the Window object method
Window.alert (): POPs up a dialog box with a OK button
Window.confirm (): POPs up a dialog box with a confirmation and Cancel button
Window.prompt (): POPs up a dialog box with an input box
window.open (): Open a new window
Window.close (): Close browser
History.go (-1): Back
History.go (0): Refresh
History.go (1): Forward
History.back (): Back
History.forward (): Forward
Six: Flexible use of regular expression matching characters
1> Pattern Match:
\: Escape character For example: \b escaped B
^: Regular expression Start symbol
$: Regular expression End symbol
*: matches the preceding character 0 or n times
+: Matches the preceding character 1 or n times
?: matches the previous character 0 or 1 times
.: match all individual characters except line break
|: or meaning, for example x|y matches x or y
{n}: matches the preceding n characters
{n,m}: matches at least n maximum of M preceding characters
[XYZ]: matches any one of the characters in brackets
[^XYZ]: match any character except the brackets is equivalent to [0-9]
\w: matches any number or letter or underscore equivalent to [a-za-z0-9_]
\d: Matches a number between any 0--9
I: Ignore case
2>js: Regular detect string function: Test (), success returns TRUE, Failure returns false
For example:
Var str= "Zhangsan";
Var reg=/^\w+$/;
If (Reg.test ($str)) {
Alert ("User name is legal")
}
3> JS Regular expression:
The user name consists of a 6-18-digit alphanumeric underscore and cannot be preceded by a number
r_name=/^[a-z]\w{5,17}$/i
Password length cannot be less than six bits
var r_pwd=/^\w{6,}$/
All General e-mail addresses
var r_eamil=/^\[email protected]\w+ (\.) \w+$/
Match a QQ email address
[Email protected]
var r_qq_email=/^\d{5,} @qq (\.) com$/
Match a 163 e-mail address
var r_163_email=/^\[email protected] (\.) com$/
Matching a suffix name may be. com|.net|.cn|.edu
var email=/^\[email protected]\w+ (\.) com|net|cn|edu$/
Requires a valid age period
var r_age=/^\d{1,2}$/
if (age>=18&&age<=100)
Verify phone Number: 11 bit 13 15 18 start
var r_tel=/^1[3,5,8]\d{9}$/
Verify that the ID number is 18 bits or 17 digits plus an X
var r_s=/^\d{18}|\d{17}x$/i
Verify Chinese
var reg=/^[\u4e00-\u9fa5]{2,17}$/
4>php Regular Expression:
PHP validates the regular expression of the function preg_match ();
$reg = "/^\d{6}$/";
$str = "123456";
if (Preg_match ($reg, $str)) {
echo "OK";
}else{
echo "No";
}
Declaration method: $ Variable name = "/^ regular expression $/"; And JS is the only difference is the expression to add ""
$reg = "/^[\x{4e00}-\x{9fa5}]+$/u"; Chinese expressions
Seven, Cookie and session usage:
1> Steps to use cookies:
1) Set Cookie:setcookie (' Cookie name ', ' cookie value ', Expiration Time)
2) Read the cookie:$_cookie[' COOKIE name ']
3) Delete cookies:
Setcookie ("The name of the cookie to be deleted", "", Time ()-1)
Browser Delete Manually
4) Classification of cookies:
is divided into session cookies and persistent cookies, session cookies are not set to the cookie expiration time,
When you close your browser or turn off your computer, cookies will automatically disappear; persistent cookies set the expiration time for cookies.
As long as the expiration time is not reached, close the browser and turn off the computer cookies will not disappear, only the expiration time to disappear.
Use of 2>session:
1) Open Session:session_start (); Cannot have any output before this function
2) Assign value to SESSION: $_session[' name ']= value
3) Use session:$ variable name =$_session[' name ']
4) Delete session:
Delete a single session:unset ($_session[' name ')
Delete multiple Session:$_sesson=array ();
End current All Session:session_destroy ()
5) How the session works
Definition of Session: Session is a mechanism that stores server-side sessions to track and identify user information.
When a session is started, a random and unique session_id is generated, which is the session filename, at which point the
The session_id is stored in a local cookie. This ID is automatically logged off when the page is closed, and a random ID is generated again when you log on to the page again.
6) Understand the difference between session and Cookie
Difference: Session stored on server side, cookie stored on client, session relative to security, cookie unsafe, session and
The cookie can set the expiration time, can pass the value across the page, can realize the session function. After the session is opened, there is a sessionid stored in
Cookie, if the client prohibits Cookie,sessionid from being transmitted as a URL address bar.
3> session control for user login using session or cookie
The cookie implements the login control: Setcookie ("name", "value", "Expiration Time"), after the setting is completed, after each login to access the page to join:
if (Empty ($_cookie[' name '))) {die ("not logged in, can't operate!")}, if the user has chosen seven-day free login, add in the login interface:
if (!empty ($_cookie[' name ')) {header ("Jump")}
Session Realization Landing Control: Session_set_cookie_params (1*60); Session_Start (); $_session[' name ']= value;, after Setup is complete,
After each login to access the page to join: if (empty ($_session['))) {die ("not logged in, cannot operate!") ")}, if the user chooses a seven-day free login,
Add: if (!empty ($_session[' name ')) {header ("Jump")} in Login interface
Eight: Upload and download
1 Upload: Move_uploaded_file (): Moves the uploaded file to the specified location and returns true if successful, otherwise false
2 Download: <a href= ' dowload.php?path= location of the file ' > download </a>
dowload.php
<?php
Receive file path
$path = $_get[' path '];
File type
Header (' Content-type:image/jpeg ');
Activating a downloaded window (file name)
Header ("content-disposition:attachment; Filename= $filename ");
Read the file
ReadFile ($path);

JavaScript Basics (2)

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.