Home > Web Front-end > JS Tutorial > body text

JavaScript explained in detail

高洛峰
Release: 2016-11-26 09:05:40
Original
944 people have browsed it

JavaScript can be said to be an essential technology in web development. It is flexible, simple and efficient. This time DRP used a lot of js, which gave me a deeper understanding of js. After watching this, I went back and watched the JS video of beef brisket. I looked at things I hadn’t seen before. Here is a summary of js. S What is JavaScript? What are the characteristics of JS? JS is an interpreted web script language. Its role is to control the behavior and content of the browser.
js code is embedded in Html.
It has several major features
It runs on the client, which means it can reduce the pressure on the server. Improve code execution efficiency.
JavaScript is an interpreted language (that is, code execution is not pre-compiled)
The corresponding compiled language needs to be compiled into intermediate code or machine language in advance during execution, such as java compiled into a .class file.
javaScript has nothing to do with java. Javascript is not a stripped-down version of other languages ​​(it is only vaguely and indirectly related to Java, for example), nor is it a simplification of anything. However, it has its limitations. For example, you cannot use the language to write standalone applications, and there is no built-in support for reading and writing files. In addition, Javascript scripts can only run on a certain interpreter or "host", such as Active Server Pages (ASP), Internet Browser, or Windows Script Host.
What functions can JavaScript achieve?
JavaScript can implement browser script development. It has many classic features. We need to master.
JavaSript implements web page special effects
Js can implement many web page special effects, such as scrolling of picture text, dynamic transformation of pictures, etc. Searching the Internet, you can find a lot of ready-made code. In short, there are many things that you don’t need to do yourself. You need to stand on the shoulders of giants.

JavaScript operates Html elements
Js can operate web page elements, such as input boxes, drop-down boxes, tables and other elements. The basis for operating html elements is the operation of DOM objects. Any html element is a dom object. We can use js to operate it. For example, change its attributes and dynamically add elements. Get its value etc.
Each element in Html corresponds to a node in dom. The html form corresponds to a dom tree, and each node has nodeName nodeValue nodeType attributes. js operates html by operating these attributes.
Here are some operation methods.
createElement(element): Create a new element node with the specified tag name, and the return value is a reference pointer to the new element node.
createTextNode(string): Create a new text node containing the given text, and return a pointer to the new element node. Reference pointer of text node:
appendChild(node): insert node
removeChild(node): will remove a child node from a given element and return a reference pointer to the deleted child node.
hasChildNodes: used to check whether a given element has child nodes and returns a boolean value
replaceChild(newnode,oldnode): node replacement
setAttribute(key, value): add a new attribute value or change the given element node Its existing attributes
getAttribute(key): Returns the value of a given attribute node of a given element
getElementById(): Finds an element with a given id attribute value and returns an element node
getElementsByTagName(): Use To find all elements with a given tag name
As can be seen from these methods, js can flexibly add, delete, and obtain a node (html element). In this way, the operation of the form will be much more flexible.
In this drp, the main method used is getElementById().
For example:
with(document.getElementById("userform")){
action="user_maint.jsp";
method="post";
submit();
}

JavaScript implements form validation

by JavaScript Typical form data for verification include:

Has the user filled in the required fields in the form?
Is the email address entered by the user legal?
Has the user entered a valid date?
Did the user enter text in the numeric field?
There are usually two ways to validate a JS form. One is to write a manual method. When the form is submitted or an event is triggered, the js method is called, and then the properties of the control are extracted through the js code to determine whether it meets the conditions. Another method is to use regular expressions to verify whether the input data conforms to a certain format. For example, the following js code for verification email

www.2cto.com

function test() 
                                                                                                                                                                                                    
var myreg =/^([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[ _|_|.]?)*[a-zA-Z0-9]+.[a-zA-Z]{2,3}$/;
                                                                                      
                   alert('Please enter a valid E_mail!');


The key to the code is to find the regular expression, and the second is to use this regular expression The test method is used to verify a certain string. Note that the test method comes with this regular expression.
JavaScript checks the customer's browser and creates cookies
Use javaScript to verify the user's browser version, information, etc.
The Navigator object below contains information about the visitor's browser, including browser type, version, etc.
"+ browser)

document.write("
")

document.write("Browserversion: "+ version)






A cookie is a variable stored on the visitor's computer. This cookie is sent each time the same computer requests a page through a browser. You can use JavaScript to create and retrieve cookie values.
First: Create a function that stores the visitor's name in a cookie variable
functionsetCookie(c_name,value,expiredays)
{
varexdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document. cookie=c_name+"=" +escape(value)+
((expiredays==null)? "" : ";expires="+exdate.toGMTString())
}
Secondly: Create another function to check if it has been set cookie:
functiongetCookie(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
returnunescape(document.cookie.substring(c_start, c_end))
}
}
return""
}


The above function will first check whether there is a cookie in the document.cookie object. If the document.cookie object stores certain cookies, it will continue to check whether the cookies we specify have been stored. If the cookie we want is found, a value is returned, otherwise an empty string is returned.

In short, js is an essential technology in web development. As a web developer, you must be familiar with it.



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template