简体   繁体   English

我们可以将任何类型的javascript代码用作外部.js文件,还是有时需要将其放置在 <head> ?

[英]Can we use any type of javascript code as a external .js file or sometime it's necessary to place in <head>?

我们可以使用任何类型的javascript代码作为外部.js文件,还是有时需要将其放置在<head>

The only time you would ever need to inline a js function in your HTML using the <SCRIPT> tags is if your javascript is generated by your server side program depending on the data, user settings etc. 您唯一需要使用<SCRIPT>标记在HTML中内联js函数的时间是,如果您的JavaScript是由服务器端程序根据数据,用户设置等生成的。

Even this case is extemely rare as as you should be able to create a .js function whose behaviour is controlled by passing parameters. 即使这种情况也很少见,因为您应该能够创建一个.js函数,其行为由传递参数来控制。

Apart from keeping everything tidy and in the place where you expect to find it, there is a network performance advantage in that *.js files are cached on the client side so you are not constantly sending the same stuff over the network again and again. 除了将所有内容保持整洁并放在期望的位置外,还有网络性能优势,因为* .js文件缓存在客户端,因此您不必不断地在网络上一次又一次地发送相同的内容。

You can place it all in an external file. 您可以将它们全部放置在外部文件中。 It's much cleaner, and easier to maintain. 它更清洁,更易于维护。 It's a good practice to keep Javascript and CSS in their own external files. 将Javascript和CSS保留在自己的外部文件中是一个好习惯。 Do away with inline switching between CSS, HTML and Javascript for a much better organized project and less frustration down the road. 不必在CSS,HTML和Javascript之间进行内联切换,从而可以更好地组织项目,并减少挫败感。

如果使用jQuery,则$.document.ready()是一种方法

Unless it is a Dynamic content which is placed by the server side scripts (very rarely used as there are many more better alternative methods) .. You can use JS in an external file .. 除非它是由服务器端脚本放置的动态内容(很少使用,因为有许多更好的替代方法)..您可以在外部文件中使用JS。

External js is re-usable .. I mean can be used by more than one HTML page .. So obviously it brings down the burden on browser .. 外部js是可重用的..我的意思是可以被多个HTML页面使用..因此显然减轻了浏览器的负担..

The site providing the live telecast or the NEWS/information (example:cricket scores etc) real-time examples for Dynamic content .. 该站点提供动态内容的实时电视广播或新闻/信息(例如:板球得分等)实时示例。

暂无
暂无

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

相关问题 为什么我们不应该在之间放置任何javascript代码<script> tag if we only want to include external js file? - why shouldn't we put any javascript code between the <script> tag if we only want to include external js file? 我们可以使用 System.import 或 import() function 到任何 Javascript 文件还是仅使用单个模块特定的 js? - Can we use System.import or import() function to any Javascript file or only Single module specific js? 我可以使用任何 HTML 或 JavaScript API 来获取 input[type=file] 中的文件路径吗? - Can I use any HTML or JavaScript API to get the file's path in input[type=file]? 如何在具有 twig 元素的外部文件中使用 javascript 代码? - How can I use javascript code in an external file with twig elements? 如何将文本放入在外部javascript文件.js中创建的div中 - How to place text into div created in external javascript file .js 在 JavaScript 中,我们可以在<script></script>标签而不加载外部文件? - In JavaScript, can we import a JS module in the <script></script> tag without loading an external file? 我们可以将 JavaScript 代码沿 c# 放入.NetCore WebApi - Can we place JavaScript Code along c# in .NetCore WebApi 外部 js 文件中的 Reactjs 组件有时无法加载 - Reactjs component in an external js file does not load sometime 我们可以在没有任何外部数据库的情况下将 kafka 与 nodejs 一起使用吗? - can we use kafka with nodejs without any external database? 如何使用在浏览器的JavaScript控制台的某些外部* .js文件中定义的javascript函数? - How to use javascript functions, defined in some external *.js file in browser's javascript console?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM