简体   繁体   English

JavaScript和HTML:在两个单独的文件中还是在一个HTML文件中?

[英]JavaScript and HTML : in two separate files or in a single HTML file?

Is there any difference when we write both HTML and JavaScript code in a single .html file or write HTML code in a .html file and JavaScript in a .js file separately ? 有什么不同的时候,我们都写HTMLJavaScript代码在一个单一.html文件或写HTML的代码.html文件和JavaScript一个.js单独的文件? I mean functionality in both cases is the same ? 我的意思是两种情况下的功能是否相同?

ex. 恩。 here : https://github.com/tomconte/solarchain-dashboard HTML and JavaScript are written in two separate .html and .js files and in top of .js file is mentioned: NOTE: Need to compile with browserify viz.js -o main.js . 在这里: https://github.com/tomconte/solarchain-dashboard HTMLJavaScript都写在两个不同.html.js文件,并在顶部.js文件中提到: 注:需要编译browserify viz.js - o main.js。

I mean if JavaScript code in viz.js file is written in index.html file, do we still need to use browserify to compile JavaScript code ? 我的意思是,如果viz.js文件中的JavaScript代码写在index.html文件中,是否还需要使用browserify来编译JavaScript代码?

index.html file : https://github.com/tomconte/solarchain-dashboard/blob/master/index.html index.html文件: https : //github.com/tomconte/solarchain-dashboard/blob/master/index.html

viz.js file : https://github.com/tomconte/solarchain-dashboard/blob/master/viz.js viz.js文件: https : //github.com/tomconte/solarchain-dashboard/blob/master/viz.js

To in two separate files or in a single HTML file? 在两个单独的文件中还是在一个HTML文件中?

No difference. 没有不同。

Just browser has to send an extra http request to load the file when you add js file in <script> tag. 当您在<script>标记中添加js文件时,只是浏览器必须发送额外的http请求以加载文件。

But it is a good practice to write your code in separate js file, so it would be easy to modify it and reflect those changes in all the html files wherever that js file is linked otherwise you'll have to change js code in every file etc etc 但是,将代码编写在单独的js文件中是一个好习惯,因此修改该文件并在链接该js文件的所有html文件中反映这些更改将非常容易,否则您将不得不在每个文件中更改js代码等

Keeping the javascript separately in a file will improve page load performance. 将javascript单独保存在文件中将提高页面加载性能。 Inlining the javascript inside the html file can block rendering of html. 将javascript内联到html文件中可以阻止html的呈现。

In the inline script, the time is taken up running the script, which might change the DOM. 在内联脚本中,运行脚本会花费时间,这可能会更改DOM。 Trying to render the DOM while it's mutating is a recipe for a mess. 尝试在DOM变异时呈现DOM是造成混乱的秘诀。 So rendering only happens at points when the JS is stalled, and therefore the DOM is stable. 因此,渲染仅在JS停顿时才发生,因此DOM是稳定的。

While waiting for an external script to download, the running of scripts is stalled, so the DOM can be rendered safely. 在等待外部脚本下载时,脚本的运行被暂停,因此可以安全地呈现DOM。 The downloaded JS won't be run until the rendering is complete. 在渲染完成之前,不会运行下载的JS。

So this is one reason the files html files are separated from javascript files. 因此,这是文件html文件与javascript文件分开的原因之一。

Also maintenance of Javascript in files will be easier than embedded in html. 而且,文件中的Javascript维护比嵌入html中更容易。

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

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