简体   繁体   English

在 html 中包含 javascript 而不是链接到外部文件有什么缺点吗?

[英]Is there a downside to including javascript in html instead of linking to an external file?

I'm thinking through ways to speed up a website I'm developing.我正在考虑加快我正在开发的网站的速度。 I know socket connections are expensive, so I was thinking... Is there any downside to including css and javascript code in the actual html/php source code as opposed to linking to it?我知道套接字连接很昂贵,所以我在想......在实际的 html/php 源代码中包含 css 和 javascript 代码而不是链接到它有什么缺点吗?

It seems that instead of making 10 calls to various files I could simply put all of the code in the html source code and not have any socket calls to external files?似乎我可以简单地将所有代码放在 html 源代码中而不是对各种文件进行 10 次调用,而不是对外部文件进行任何套接字调用?

I know I could put everything into 1 javascript file and call that, but that would still create a socket call.我知道我可以将所有内容放入 1 个 javascript 文件并调用它,但这仍然会创建一个套接字调用。

I realize this is probably not going to make much difference and might just be a thought exercise, but is there any real downside to just inlining the code?我意识到这可能不会有太大的不同,可能只是一个思考练习,但是仅仅内联代码有什么真正的缺点吗?

Different resources (ie HTML, CSS, JS, images ...) do not necessarily require a new socket connection.不同的资源(即 HTML、CSS、JS、图像……)不一定需要新的套接字连接。 With HTTP/1.1 the same connection is usually used for multiple resources (but only after each other) and with HTTP/2 multiple resources can be loaded in parallel over the same TCP connection.在 HTTP/1.1 中,同一个连接通常用于多个资源(但只能在彼此之后),而在 HTTP/2 中,多个资源可以通过同一个 TCP 连接并行加载。 Thus instead of trying to optimize delivery by combining HTML, JS, CSS into a single file it would be possible to optimize the transport instead by using HTTP/2.因此,与其尝试通过将 HTML、JS、CSS 组合到单个文件中来优化交付,不如使用 HTTP/2 来优化传输。

Apart from that often resources like script, CSS and images are shared between HTML pages.除此之外,脚本、CSS 和图像等资源通常在 HTML 页面之间共享。 In this case serving the same script etc again and again would just be wasteful.在这种情况下,一次又一次地提供相同的脚本等只会是浪费。 Proper caching instead enables reuse of shared resources between the pages.相反,适当的缓存可以重用页面之间的共享资源。

And finally, inline script is considered a security problem - just look for Cross Site Scripting .最后,内联脚本被认为是一个安全问题 - 只需查找Cross Site Scripting Having the script separated from the content allows the use of a strict Content Security Policy which prevents such attacks.将脚本与内容分开允许使用严格的内容安全策略来防止此类攻击。

It depends on different circumstances.这取决于不同的情况。 For example (size of your js file).例如(您的 js 文件的大小)。

  • If your js file size is large and having lots of codes then you should consider linking as there are many advantages -如果你的 js 文件很大并且有很多代码,那么你应该考虑链接,因为有很多优点 -

    (i) It can be cached by the browser locally so that on next visit of the user, your website will load faster. (i) 它可以由浏览器本地缓存,以便用户下次访问时,您的网站加载速度更快。

  • If your js file size is very small consider embedding it into HTML as it has many advantage too -如果您的 js 文件非常小,请考虑将其嵌入到 HTML 中,因为它也有很多优势 -

    (i) There will be less request on your server. (i) 您的服务器上的请求会减少。

    (ii) If you want some variables to be assigned dynamically you should embed js in HTML because it cannot be done with linked js file. (ii) 如果你想动态分配一些变量,你应该在 HTML 中嵌入 js,因为它不能用链接的 js 文件完成。

and so on...等等...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM