What is js
The original purpose of the birth of javascript was to "give life to web pages".
We call this programming language scripts. They can be written in HTML and automatically executed when the page is loaded.
Scripts exist and execute as plain text. They do not require special preparation or compilation to run.
What can JavaScript do in the browser?
Modern JavaScript is a "safe" language. It does not provide low-level access to memory or CPU because it was originally created for browsers and does not need these features. JavaScript's capabilities depend largely on the environment in which it is executed. For example: Node.js allows JavaScript to read and write arbitrary files, perform network requests, etc. JavaScript in the browser can do everything related to web page operations, user interaction, and web servers. For example, JavaScript in the browser can do the following:
- Insert new HTML into the web page, modify the existing web page content and the style of the web page.
- Respond to user behavior, respond to mouse clicks or movements, keyboard taps.
- Send network requests to remote servers to download or upload files (so-called AJAX and COMET technologies).
- Get or modify cookies, ask questions to visitors, send messages.
- Remember client data (local storage).
What can't JavaScript do in the browser?
For the sake of user security, the capabilities of JavaScript in the browser are limited. This is mainly to prevent evil websites from obtaining or modifying users' private data.
Examples of these limitations are:
- JavaScript in a web page cannot read, write, copy or execute files or programs on the user's disk. It has no direct access to the operating system.
Modern browsers allow JavaScript to do some file-related operations, but these operations are limited. JavaScript can only operate on the file if the user uses a certain action.
For example, "dragging" a file into the browser, or selecting a file through an input tag. JavaScript has many ways to interact with the camera/microphone or other devices, but these require the user's permission in advance. So, a web page with JavaScript enabled should not secretly start the webcam to observe you and send your information to the NSA.
- Different browser tabs are basically unrelated to each other. Sometimes, there are some relationships. For example, a tab opens another new tab via JavaScript. But even in this case, if the two tabs are not opened on the same website (domain, protocol or port), they cannot communicate with each other. This is the "same-origin policy". In order to solve the "same-origin policy" problem, both tabs must contain some special JavaScript code that handles this problem and allows data exchange, so that data exchange between two tabs of the same origin can be achieved.
- JavaScript can easily communicate with the server of the current web page domain through the Internet. But the ability to obtain data from servers of other websites/domains is limited. Although this can be achieved, it requires an explicit agreement from the remote server (in the HTTP header). This is also for the safety of user data.
What makes javascript special
- Full integration with HTML/CSS.
- Use simple tools to complete simple tasks.
- Supported by all major browsers and enabled by default.
The only browser technology that meets all three of these criteria is JavaScript.
This is why JavaScript is special! This is also why it is the most common tool for creating browser interfaces.
In addition, JavaScript also supports the creation of servers, mobile applications, etc.