JSF推迟JavaScript解析的最佳方法?

编程入门 行业动态 更新时间:2024-10-26 16:33:48
本文介绍了JSF推迟JavaScript解析的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

用JSF延迟加载JavaScript库(Richfaces,Primefaces,自己的东西)以加快页面加载的最佳方法是什么?

what is the best way to defer loading the JavaScript libraries (Richfaces, Primefaces, own stuff) with JSF to speed up page loading?

正如Google PageSpeed插件所说,建议在网站完全加载后解析JavaScript.实现此目的的一种方法是将JavaScripts加载放在<body>标记的末尾.另一种方法是将"defer"属性放到<script>标记中,这是我看到的JSF的<h:outputScript>标记无法实现的.

As Google PageSpeed plugins says it is recommend to parse JavaScript when the site is loaded completely. One way to achieve this is to put the JavaScripts loads at the end of the <body> tag. An other way is to put the "defer" attribute to <script> tag, which can not be done with JSF's <h:outputScript> Tag as I saw.

那么,你会怎么做?

推荐答案

使用<h:outputScript target="body">.然后它将结束在<h:body>的最后.也就是说,它默认为视图中的当前"位置(另一个target值为head,这将使脚本最终在<h:head>中结束,即使在<h:body>中的某个位置指定了脚本).

Use <h:outputScript target="body">. It will then end up in the very end of <h:body>. It namely defaults to "current" location in the view (another target value is head which will then make the script to end up in the <h:head>, even when the script is specified somewhere in the <h:body>).

<h:outputScript name="js/foo.js" target="body" />

如果您也想将其应用于第三方脚本,则需要创建自定义 SystemEventListener 钩在 PreRenderViewEvent ,它完全借助 UIViewRoot#getComponentResources() 和 UIViewRoot#addComponentResource() .

If you want to apply this for 3rd party scripts as well, you'd need to create a custom SystemEventListener hooking on PreRenderViewEvent which does exactly that with help of UIViewRoot#getComponentResources() and UIViewRoot#addComponentResource().

更多推荐

JSF推迟JavaScript解析的最佳方法?

本文发布于:2023-11-25 12:34:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629767.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   JSF   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!