JavaScript Grammar Basics want to learn JS friend can see the basics

Source: Internet
Author: User
Tags case statement html tags numeric value setinterval switch case
1:javascript Case Sensitive
2:javascript Each statement must be ";" End, same as C language
3: Output: document.write ("string")---> can also output the corresponding HTML markup
4: Change the color of the form document.bgcolor= "red";
4: Type conversions: Parseint,parsefloat
5: Random function: parseint (Math.random () *90+10) produces random numbers of 10--100
5: Pop-up dialog box: Alert ("Content of the hint")
5:if If...else,for,while,switch Case
5: How to define an array:
1 One-dimensional array:
A=new Array (); [does not need to specify length when defining arrays]
A[0]=1;
A[1]=1;
A[2]=1;
s=0;
for (i=0;i<a.length;i++)
{
S+=a[i];
}
2) Two-dimensional arrays:
City=new Array ();
City[0]=new Array ("Hubei province", "Wuhan");
City[1]=new Array ("Hubei province", "Xiantao");
City[2]=new Array ("Hubei province", "Honghu Lake");
City[3]=new Array ("Fujian province", "Guangzhou");
City[4]=new Array ("Fujian province", "Xiamen");
City[5]=new Array ("Fujian province", "Zhangzhou");
Functions within the 5:javascript, the invocation of functions, and the scope of the variables
6: Pop-up Query Dialog box: Confirm ("Inquiry content")
7: Close the form: Window.opener=null;window.close ();
8: Open a form:
1 Variable name =window.open ("page name") Open a new window
2 Variable name =window.open ("Web name", "name", "height=200px,width=300px")
3 Open the window in the center of the screen pop-up
T=window.open (' dotest.htm ', ' Test ', ' height=400px,width=500px ');
T.moveto ((screen.width-500)/2, (screen.height-400)/2;
4) window.location= "url" does not open a new window
5) Modal form pop-up
window.showModalDialog (' dotest.htm ', ', ', ' dialogwidth=600px;dialogheight=500px ');
9: Refresh a form:
Window.location.reload ();
10: Get the value of the form element of the forms: Table single-name. element name. Value
11: How to access the form elements of the previous form in another form
A: Modal form:
SOURCE Form
1) window.showmodaldialog (' dotest.htm ', window, ' dialogwidth=600px;dialogheight=500px ');
Note that the name must be written on window
2) Window.dialogArguments.form1.txtuser.value
B: Non-modal forms:
SOURCE form:
window.open
Destination form:
Window.opener. Table Single-name. Form element name. value
12: How to return a value to the parent form through a modal form:
SOURCE form:
T=window.showmodaldialog (Parameters)
Alert (t)
Destination form:
window.returnvalue= value; window.opener=null;window.close ();
13: How to refresh the parent form while closing the subform
A Non-modal window
SOURCE page:
window.open ("page")
Destination page:
Window.opener.location.href=window.opener.location.href;
Window.opener=null;window.close ();
B) modal window
SOURCE page:
window.showModalDialog ()-------The purpose of having a suspend code
Window.location.reload ();
Destination page:
Window.opener=null;window.close ();
14: Set the status bar text: Window.status
15 Convert String to numeric value: parseint ("string"), Parsefloat ("string")
16) Get the current time
var date=new date ();
document.write (Date.tolocaletimestring ());
17): Get the current date:
var date=new date ();
document.write (Date.tolocaledatestring ());
18): Back to the previous page. Note not refreshing
History.go (-1)
19: Change the background color of a pair of image
This.style.backgroundcolor= ' Yellow ',
this.style.color= ' text color '
20: Set as the homepage:
This.style.behavior= ' url (#default #homepage) '; This.sethomepage (' Your Web page ');
21): Specifies that the code automatically repeats a process after a few minutes.
SetInterval ("JS code", 1000)
For example: Make a background change constantly [the page flashes very badly]
var index=1;
function Changepic ()
{
form1.p1.src=index+ ". jpg";
index=index+1;
if (index==4)
{
Index=1;
}
}
SetInterval ("Changepic (Index)", 1000);
Improvement: [first define the array, the picture pre-installed in memory]
Pic=new Array (4);
Pic[0]=new Image ();
Pic[1]=new Image ();
Pic[2]=new Image ();
Pic[3]=new Image ();
Pic[0].src= "1.jpg";
Pic[1].src= "2.jpg";
Pic[2].src= "3.jpg";
Pic[3].src= "4.jpg";
function cleartext ()
{
FORM1.P1.SRC=PIC[INDEX].SRC;
index=index+1;
if (index==4)
{
Index=1;
}
}
SetInterval ("cleartext (Index)", 1000);
23: Let the specified code after how much time to execute, but only once:
settimeout ("JS code", 1000);
24): Empty the text of all the text boxes in a form
for (i=0;i<form1.elements.length;i++)
{
if (form1.elements[i].type== "text")
{
Form1.elements[i].value= "";
}
}
25 Run an executable file:
Obj=new ActiveXObject ("Wscript.Shell");
Obj.run ("Calc.exe");
() events in Java script
A) Onmouseove: Mouse Arrival
B) onmouseout: Mouse Away Event
C) OnClick: Click event
D) OnKeypress: When the key is pressed, you can get the ASII code of the key by Event.keycode
E) Load event: Writing the code directly in <script></script> is equivalent to the Load event
F) OnSubmit: When the form is submitted, it touches the publication submission event
Principle: When the user presses the Submit button, it touches the onsubmit event of the published order. In this event, depending on the value returned by the user (True,false) to determine whether the form needs to be submitted, is committed when True, false is not committed so we often use a function for data validation.
Example:
1) <marquee scrollamount=3 onmouseover=this.stop (); Onmouseout=this.start ();> text movement
2) <input Type=text name=txtpostalcode onkeypress= "If" (Event.keycode < | | Event.keycode >) event.keycode=0; ">//can only enter numbers
3 Make the selected row of the table appear in a different color
4 prevents users from entering values in the text box:
Onkeypress= "Reuturn false"
Note: In any event of a FORM element, this event is not triggered as long as you add return False
25: Regular Expression:
Like the wildcard character in DOS, used to detect whether an input satisfies a specific wildcard character.
^: Represents the beginning of a line of characters
$: To represent the end of a line of characters
[]: Used to define acceptable characters
[A-Z]: Indicates that lowercase letters can be accepted
[A-Z]: Indicates that uppercase letters can be accepted
[0-9]: The expression can accept the number
[0-9,_,a]: Indicates that a number can be accepted, underlined or letter A
[a-za-z0-9]: means to accept both English letters and numbers
[^]: unacceptable characters
[^a-z]: cannot accept English letter
{}: Used to define the number of characters that must be entered
{3}: must contain 3 characters.
{4,8}: Contains at least 4 8 characters
[0-9] {3}: must enter 3 digits
[A-za-z] {4,6}: Must enter 4 to 6-digit English letters
{N,}: Indicates that you can enter at least n characters.
[A-z] {0,}: Indicates that 0 or more English letters can be accepted
[A-z] {1,}: means to accept at least 1 letters in English
+: 1 or more times-----match the preceding character is equal to {1,}
*: 0 or more times-----match the preceding character is equal to {0,}
.: Denotes any character
Example:
Frm1.user.value.match ("^[0-9a-za-z]{5,8}$")
26: How to use JS to control the form elements:
A: Text box:
1 Gets the text of the text box:
Table Single-name. Form element name. value
2) Get Focus:
Table Single-name. Form element name. Focus ()
B: Button:
1) Make the button unavailable:
Table Single-name. Button name. Disabled=true (true, unavailable; false available)
2) Make the button invisible:
Table Single-name. Button name. style.display= "None" is not visible
Table Single-name. Button name. style.display= "" Visible
if (table single-name. Button name. style.display== "") if visible
C) Radio Box:
Get the value selected by the Radio box [the name of each single box must be the same, the value is different]
For (i=0;i< an array of radio boxes. length;i++)
if (Single-box array [i].checked) break;
Returns an array of radio boxes [I].value.

D): Get the value of all tick check boxes
For (i=0;i< check box array. length;i++)
If (check box array [i].checked) executes the corresponding statement
E): Dropdown box
A) Get the selected value--> the dropdown box. Value
B Delete all contents inside-----> Dropdown box. length=0;
C Delete the specified item---> Dropdown box. Options.remove (subscript);
D Add a-----> dropdown box to the inside. options[dropdown box. Length]=new Option ("tag", "value");
Notice that the options for the dropdown box are an array that stores all the selections, and the subscript is zero-based.
E) SelectedIndex: Gets or sets the subscript for the selected item
F) Options[k].value: Get the value of item I
G) onchange Event: When the item in the election is changed, the trigger

Example: In the client implementation of the two Drop-down box linkage [note definition one-dimensional array]
City=new Array ();
City[0]=new Array ("Hubei province", "Wuhan");
City[1]=new Array ("Hubei province", "Xiantao");
City[2]=new Array ("Hubei province", "Honghu Lake");
City[3]=new Array ("Fujian province", "Guangzhou");
City[4]=new Array ("Fujian province", "Xiamen");
City[5]=new Array ("Fujian province", "Zhangzhou");
27: Create a module of the JS file, and then in the page to call
(a) Create a new *.js file directly:
Write the function directly without adding <script type= "Text/javascript" > Mark
B in the destination page, by: <script src= "Jscript.js" type= "Text/javascript" ></script> to reference
28: Multiple pairs like sharing the same event:
<script type= "Text/javascript" for= "TabItem" event= "onmouseover" >
var tabitem=document.getelementsbyname ("TabItem");
for (i=0;i<tabitem.length;i++)
{
if (tabitem[i]==this)
{
Tabitem[i].background= "Images/nh-bg.gif";
}
Else
{
Tabitem[i].background= "";
}
}
</script>

Fourth chapter: C#.net Grammar Foundation
In this chapter, you will learn some of the following
1: The choice of writing asp.net language
2:vs.net 2005 Interface Tips
3: How to add server-side code to the page
4: How to import namespaces in a page
5:c#.net Grammar Basics
6: Dynamically adding JavaScript to the client from the server side


To write the choice of asp.net language:

Write asp.net program, NET provides us with the following languages vb.net,c#.net,j#.net which vb.net language is the simplest and easiest language to learn, it inherits most of VB syntax, while adding a whole set of. NET Framework, Using vb.net to develop asp.net programs is the easiest language c#.net is the entire. NET nuclear language, which inherits most of the syntax of c,c++ and is somewhat more complex than vb.net, but executes programs more efficiently than VB.net, J #. NET inherits most of the syntax of JavaScript and is rarely used. We choose C#.net language to develop the language of ASP.net program

Vs.net 2005 Interface Tips
1 Set Display solution---------: Tools-Options--> projects and Solutions--> General
2 to a single page can be generated, do not need to build the entire project
3 design mode and source file mode (HTML mode), after code mode (class), so that the page when the load automatically display design mode
4 Document Outline window: You can clearly understand the hierarchical relationship of HTML tags: view--> other Windows---> Document Outline
5 HTML tag navigation: switch to the source file mode, single right--> select the bottom of the "Selected HTML tag"
6 control drag in source file mode
7 Multi-Document page display------ctrl+tab can switch between different documents
8) ASP.net website special folder:
A:app_code used to store code files (e.g., *.cs, class files)
B:app_date used to store Web site data files (database files, XML files, etc.)
C: There are a lot of other special folders
9) Program code refactoring:
A: Refactoring Properties
B: Refactoring Methods
Dynamic compilation of asp.net Web site:
A: When ASP.net first runs, IIS automatically generates a DLL for asp.net, so the first time is very slow
As soon as the contents of the file have not changed, IIS will be able to reuse the DLL that was generated last time and will not generate
New DLL, so first run slow, later run fast
B: If the contents of the source file of ASP.net change, IIS will be born again in a DLL, using this feature
We can write programs in the Vs.net environment, and then refresh them directly after we open the Web site in IIS.

How to add server-side code to a page
C#.net can only be compiled by server-side IIS, so the C#.net language must be run on the server side
A: Adding code directly to the "Post code file" event [using Codebehind]
B: Directly to the code into the "page file" inside this time must add <%%> to limit [using Codebeside]
1 if it is simple C # code, you can use the <%%> around, and can have more than one <%%>
2 If it is a function, be sure to follow the <%page%> command, and follow the following format:
<script language= "C #" runat= "Server" >
private int sum (int a, int b)
{
return a + B;
}
</script>
3 If you want to get the value of a variable, you can write the <%= variable name%>
Like what:
Today is: <%=system.datetime.today.tolongdatestring ()%>

How to import namespaces in a page
1 in the code file: Use statement
2 in the paging file: Use the <%@ Import statement for example:
<%@ Import namespace= "System.Data.SqlClient"%>
The location is under <@ page>.


The grammatical basis of c#.net
C#.net Data type:
Int,double,string,char,object data type (equivalent to the variant type in VB)
To define a variable:
The name of the type identifier variable;
To assign an initial value to a variable:
You can assign an initial value to a variable at the time of definition---Int a=5;
You can also assign an initial value to a variable after you define it
Operator:
A): assignment operator: =
b): arithmetic operator: +,-, *,\ (divisible),% (remainder)
c): String join character: +
D): relational operator: >,>=,<,<=,==
E): logical operator: &&,| |,!
f): Composite operators: x+=3,x*=6, etc.

asp.net input and output:
A): Output
1): Output simple string: Response.Write ("string");
2): Output HTML Tags: response.write ("HTML tags") [Important]
For example: Response.Write ("<a href= ' C:\WINNT\Web\Wallpaper\ castle. jpg ' > My links </a>")
3): Output JS script: Response.Write ("<script>js code </script>");
4): Output the current date and time:
Response.Write (DateTime.Now.ToLongTimeString ()): Time
Response.Write (DateTime.Now.ToLongDateString ()): Date
b): Input: Use the control for input, such as TextBox control, etc.

If statement, select Case statement
Example:
A) Let the user input two numbers and an operator to find the result of the operation
B let the user input three classes to find out the average score and total score and based on the results of the determination of superior, good, poor
(C) Replace the corresponding background picture with a "week" according to the week

Loop statement: For,while Loop
Example:
A) output Moon1.gif~moon8.gif These files inside the picture
B use the table to print the 99 multiplication table
C implementation of Dice game
7: Array:
1): Declaration and initialization
Declaration and initialization of a static array:
string[] NameList = new String[6];
is initialized
String[] Namelist=new string[5] {"John", "Dick", "Harry", "Zhao Liu", "Wang Qi"};
Initialization of a dynamic array:
String[] Namelist=new string[] {"John", "Dick", "Harry", "Zhao Liu", "Wang Qi"};
2): Array. Length: Used to read the lengths of the sets
Example:
A to define names, languages, mathematics, and chemical arrays, and to find out the total score and average points through functions and tables
B use arrays and tables to print out lists of items

8: Hash Table (HashTable): is a two-column data structure. A column is a key (key), a column is a value
To create a hash table:
Hashtable has = new Hashtable ();
Add Data:
Has.add ("key", value)
Gets the value of the specified key
has["Key"]
Check if a key already exists
Has. ContainsKey ("key")
Delete a key
Has. Remove ("key")
Clear All Keys
Has.clear ();
To access the contents of a hash table:
Hashtable ht = new Hashtable ();
foreach (DictionaryEntry item in HT)

26: Dynamic to the server (Web control) to join the client javascript[important]
The previous approach was based on a written function and then called to the client, but sometimes it might require a server
Data from the end, and then processing at the client, this time will be dynamic to the client to add code
Method:
In the Page_Load event, write:
1:registerclientscriptblock ("Chen", JS) method to register JS
2:isclientscriptblockregistered ("Chen") method to check whether some JS has been registered, so
Can avoid a JS is repeatedly registered
Example:
1 from the database to read out the staff information, into the client's JS. Forms such as:
Worker=new Array ()
Worker[0]=new Array (' John ', ' BM1 ');
Worker[1]=new Array (' Dick ', ' BM1 ');
Worker[2]=new Array (' Harry ', ' BM2 ');
Worker[3]=new Array (' Zhao Liu ', ' BM2 ');
2 through the database, to achieve the two Drop-down box linkage (no refresh)
Related Article

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.