[英]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.