简体   繁体   English

有条件地添加删除<script>, <link> nodes from <head>

[英]Conditionally add remove <script>, <link> nodes from <head>

I have a web project which dynamically loads many javascript code. 我有一个网络项目,可以动态加载许多javascript代码。 This code needs additional includes (links to external js and css files in head). 此代码需要其他包含(指向外部js和CSS文件的链接)。 These includes are needed only at the beginning of user experiance on my site. 这些内容仅在我的网站上开始用户体验时才需要。 Some kind of configuration. 某种配置。 Therefore currently I included - hard coded in the main page's head node - all needed script and link nodes. 因此,目前我包括了所有必需的脚本和链接节点,这些代码都硬编码在主页的头节点中。

So, do I get any benefits if I maintain only currently needed script link in header (yes, I preferre they appear in header, not in body)? 因此,如果仅在标头中维护当前所需的脚本链接(是的,我希望它们显示在标头中,而不是在正文中),我可以获得任何好处吗? Does these nodes add additional load on my page loading? 这些节点是否在页面加载中增加了额外的负载?

And, second question, how can I conditionaly add remove script link nodes in order to support only those of them which are really needed at the given moment during the page lifetime? 第二个问题是,我如何有条件地添加删除脚本链接节点,以便仅支持页面生命周期中在给定时刻真正需要的那些节点?

... and a last question how do you think about grouping script link node inside a div (within a head)? ...还有最后一个问题,您如何看待div(在头部内)内的脚本链接节点分组?

It will slow your page down by having having many includes. 有许多包含内容会减慢您的页面速度。 These are additional calls to the server that could be avoided. 这些是可以避免的对服务器的附加调用。 You could configure you header section so that it is included on every page but has conditions written in so that only the includes required are loaded using conditional if statements. 您可以配置标头部分,使其包含在每个页面中,但要写入条件,以便使用条件if语句仅加载所需的包含。

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

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