JSF推迟JavaScript解析的最佳方法? [英] Best way for JSF to defer parsing of JavaScript?

查看:110
本文介绍了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" />

如果您也想将其应用于第三方脚本,则需要创建自定义 PreRenderViewEvent ,它完全借助

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解析的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆