如何浏览器处理JavaScript? [英] How do browsers handle JavaScript?

查看:174
本文介绍了如何浏览器处理JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何Web浏览器处理的网页的JavaScript内容?是JavaScript的内容被解析成DOM并呈现?

How does a web browser handle the JavaScript content of a webpage? Is the JavaScript content being parsed into a DOM and then rendered?

我并不需要一个规范,但我需要知道它是如何做。请告诉我处理网页上的JavaScript内容的整个过程。

I do not need a specification, but I need to know how it is done. Please tell me the whole process of handling JavaScript content on a web page.

推荐答案

网页的剧本部分由浏览器的JavaScript间preTER处理的,可能是浏览器的一个内在组成部分,但通常是一种独特的模块,有时甚至是一个完全不同的项目(Chrome使用V8; IE使用JScript,Firefox使用SpiderMonkey的;等等)。

The script sections of a web page are handled by the browser's JavaScript interpreter, which may be an intrinsic part of the browser but usually is a distinct module, sometimes even a completely distinct project (Chrome uses V8; IE uses JScript; Firefox uses SpiderMonkey; etc.).

在HTML解析器达到剧本元素,所有解析器不会被读取和文本通过结束<$ C $存储C>&LT; / SCRIPT&GT; 标签(或检索通过的src 属性引用的文件)。然后,除非作者已经使用 延迟异步 属性,所有的HTML解析和渲染来个急刹车和HTML解析器手中脚本文本关闭的JavaScript的跨preTER。 JavaScript的跨preTER间$ P $点JavaScript的code在窗口的上下文对象,做返回的HTML解析器的时候特别然后继续解析和显示该网页。此停止一切和运行的非JavaScript是为什么一些突出以人建议把脚本在底部该页面以提高感知的加载时间。这也意味着,剧本标记处理的的,它可以是重要的,如果一个脚本,依赖于另一个。如果使用了延迟异步属性,执行脚本可以被推迟,直到后来在支持它的浏览器。网页上的所有脚本相同的全局执行上下文内执行,共享同一个全局命名空间和存储器区域(并且因此可以彼此相互作用)。

When the HTML parser reaches a script element, all that the parser does is read and store the text through the ending </script> tag (or retrieve the file referenced via the src attribute). Then, unless the author has used the defer or async attributes, all HTML parsing and rendering comes to a screeching halt and the HTML parser hands the script text off to the JavaScript interpreter. The JavaScript interpreter interprets the JavaScript code in the context of the window object, and when done returns to the HTML parser, which can then continue parsing and displaying the page. This stop-everything-and-run-the-JavaScript is why some prominent people recommend putting scripts at the bottom of the page to improve the perceived load time. It also means that script tags are processed in order, which can be important if one script relies on another. If the defer or async attribute is used, script execution can be deferred until later on browsers that support it. All scripts on the page are executed within the same global execution context, sharing the same global namespace and memory area (and thus can interact with one another).

在页面解析和渲染,可能会出现&NBSP各种活动;&MDASH;用户可以点击的东西,浏览器窗口可以调整大小,鼠标可以在移动的元素。 JavaScript的code已运行作为在脚本是一个结果标签可以钩到这些事件,要求在浏览器中调用一个函数在JavaScript中,当发生事件。这允许JavaScript是互动&NBSP;&MDASH;用户点击某个元素在页面上,比如,浏览器会告诉它应该运行功能 X 中的JavaScript code JavaScript的跨preTER。

Once the page is parsed and rendered, a variety of events can occur — the user can click something, the browser window can be resized, the mouse can move over elements. JavaScript code that was run as a result of being in a script tag can "hook into" these events, requesting that the browser call a function in the JavaScript when the event occurs. This allows JavaScript to be interactive — the user clicks an element on the page, for instance, and the browser tells the JavaScript interpreter that it should run function X in the JavaScript code.

正如你可以在上面看到,有其中的JavaScript code可运行两个稍微不同的情况:在页面解析/渲染处理(当剧本不使用元素延迟异步属性正在初步处理),和的之后解析/渲染处理(延迟脚本和code运行以响应事件)。在解析/渲染过程中运行的JavaScript可以直接输出内容,通过 HTML解析器的document.write 功能。的JavaScript解析/渲染运行后完成不能做到这一点,当然,但可以使用非常强大的 HTML DOM API 与DOM进行交互。

As you can see above, there are two somewhat different situations in which JavaScript code can be run: During the page parsing/rendering process (when a script element that does not use the defer or async attributes is being initially processed), and after the parsing/rendering process (deferred scripts, and code running in response to an event). JavaScript running during the parsing/rendering process can directly output content to the HTML parser via the document.write function. JavaScript running after the parsing/rendering is complete can't do that, of course, but can use the very powerful DOM HTML API to interact with the DOM.

这也许值得一提的 无脚本 元素:与JavaScript的浏览器启用无脚本元素完全跳过。在没有JavaScript或禁用JavaScript浏览器,在剧本元素是完全跳过,无脚本元素读来代替。这使得它容易包含将被显示只有JavaScript是或不是,启用了当页面呈现在浏览器内容。

It's probably worth noting the noscript element: In a browser with JavaScript enabled, noscript elements are completely skipped. In a browser without JavaScript or with JavaScript disabled, the script elements are completely skipped and the noscript elements are read instead. This makes it easy to include content that will be shown only if JavaScript is, or is not, enabled on the browser when the page is rendered.

推荐阅读:

这篇关于如何浏览器处理JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆