Home >Web Front-end >JS Tutorial >What is the role of writing js in web pages?
What can JavaScript do?
Events can be used to handle form validation, user input, user behavior and browser actions: events are triggered when the page is loaded; events are triggered when the page is closed; the user clicks the button to perform the action; verify the user input content Legality...
2. At the same time, we can also use a variety of methods to execute JavaScript event code: HTML event attributes can directly execute JavaScript code; HTML event attributes can call JavaScript functions; you can specify yourself for HTML elements Event handler; can prevent events from happening...
Recommended course: Js tutorial.
So, we can summarize
JavaScript can change all the HTML elements in the page
JavaScript can change all the HTML elements in the page HTML attributes
JavaScript can change all CSS styles in the page
JavaScript can react to all events in the page
Features
JavaScript script language has the following characteristics:
(1) Script language. JavaScript is an interpreted scripting language. Languages such as C and C are compiled first and then executed, while JavaScript is interpreted line by line during the running of the program.
(2) Based on objects. JavaScript is an object-based scripting language that can not only create objects but also use existing objects.
(3)Simple. The JavaScript language uses weakly typed variable types and does not impose strict requirements on the data types used. It is a scripting language based on Java's basic statements and controls, and its design is simple and compact.
(4)Dynamic. JavaScript is an event-driven scripting language that can respond to user input without going through a Web server. When visiting a web page, JavaScript can directly respond to these events when the mouse is clicked, moved up or down, or moved in the window.
(5) Cross-platform. JavaScript scripting language does not depend on the operating system and only requires browser support. Therefore, after writing a JavaScript script, it can be brought to any machine for use, provided that the browser on the machine supports the JavaScript scripting language. Currently, JavaScript is supported by most browsers. [3]
Different from server-side scripting languages, such as PHP and ASP, JavaScript is mainly used as a client-side scripting language to run on the user's browser and does not require server support. Therefore, in the early days, programmers preferred JavaScript to reduce the burden on the server, but at the same time it also brought about another problem: security.
As servers become more powerful, although programmers prefer scripts running on the server to ensure security, JavaScript is still popular for its cross-platform and easy-to-use advantages. At the same time, some special functions (such as AJAX) must rely on Javascript to be supported on the client side. With the development of engines such as V8 and frameworks such as Node.js, and their features such as event-driven and asynchronous IO, JavaScript is gradually used to write server-side programs.
Example:
Find the HTML element for positioning by tag name:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>使用标签名找到HTML元素</title> </head> <body> <p>你好世界!</p> <div id="main"> <p>JS HTML DOM</p> <p> 该实例展示了 <b>getElementsByTagName</b>方法 </p> </div> <script> var x=document.getElementById("main"); var y=x.getElementsByTagName("p"); document.write('id="main"元素中的第一个段落为:' + y[0].innerHTML); </script> </body> </html>
The above is the detailed content of What is the role of writing js in web pages?. For more information, please follow other related articles on the PHP Chinese website!