页面加载后如何执行JavaScript?
我想在页面加载后执行 JavaScript 函数。目前我有一个命令按钮,一切正常。然而,如果用户不应该按下按钮,那就会更舒服。
我尝试过 f:event,但我没有侦听器,我只有 JavaScript 函数。此外,body onload 对我不起作用,因为我只使用高级组件。
<f:view xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:pm="http://primefaces.org/mobile" contentType="text/html">
<ui:composition template="/resources/master.xhtml">
<ui:define name="content">
<pm:content>
<h:inputHidden id="address" value="#{pathFinderBean.address}" />
<div id="map" style="width: 100%; height: 285px;"></div>
<p:commandButton type="button" value="Route" onclick="PathFinder.findAndGo()"/>
<div id="route"></div>
</pm:content>
</ui:define>
</ui:composition>
JavaScript 函数 PathFinder.findAndGo 在我的 master.xhtml 中定义
I would like to execute a JavaScript function after the page was loaded. At the moment I have a commandButton and everything works fine. However it would be more comfortable if the user is not supposed to hit the button.
I have tried f:event, but I do not have a listener, I have only the JavaScript function. Moreover body onload does not work for me as I use only high level components.
<f:view xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:pm="http://primefaces.org/mobile" contentType="text/html">
<ui:composition template="/resources/master.xhtml">
<ui:define name="content">
<pm:content>
<h:inputHidden id="address" value="#{pathFinderBean.address}" />
<div id="map" style="width: 100%; height: 285px;"></div>
<p:commandButton type="button" value="Route" onclick="PathFinder.findAndGo()"/>
<div id="route"></div>
</pm:content>
</ui:define>
</ui:composition>
The JavaScript function PathFinder.findAndGo is defined in my master.xhtml
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
按如下方式使用 JQuery:
更新:
它必须位于
内。Use JQuery as follows:
Update:
It has to be within
<ui:define name="content">
.当我想在 Primefaces 中加载页面后执行 JS 函数时,有两种方法对我有用:
jQuery(文档).ready(function () {
jQuery(文档).ready(函数() {
// 在 document.ready 中执行两次,在 Primefaces 回调后执行
路径查找器.findAndGo();
});
});
There are 2 ways that work for me, when I want to execute a JS function after page load in Primefaces:
jQuery(document).ready(function () {
jQuery(document).ready(function () {
// twice in document.ready to execute after Primefaces callbacks
PathFinder.findAndGo();
});
});
<p:remoteCommand oncomplete=" PathFinder.findAndGo(); " autoRun="true"/>
对于 primefaces,我成功地使用了以下内容:
我已在
中加载了包含我需要 onLoad 的函数的 JS 文件:
<前><代码>< h:outputScript库=“javascript”名称=“nameOfMyFile.js”目标=“head”>
我使用了以下内容来运行 JS 函数我需要来自“nameOfMyFile.js”:
<前><代码>< h:body onload="nameOfMyFunction()" >
请注意,我的 js 文件底部还包含以下命令:
第三点用于运行函数
onReady
。这完全是一个实验,看看我是否可以将 HTML 添加到计划事件中......因为传递的字符串被解析并且 html 标签被转义。我还没有弄清楚为什么我需要onReady
和onLoad
....但目前这是对我有用的唯一方法。如果我发现任何新内容,我会更新。成功了。
For primefaces I have managed to successfully use the following:
I have loaded in
<head>
the JS file containing the functions I needed onLoad by using:I have used the following in order to run the JS functions I needed from "nameOfMyFile.js":
Please note that my js file also contained the following command at the bottom:
The 3'rd point is for running the function
onReady
. This was all an experiment to see if I can add HTML to the schedule events ... as the strings passed there are parsed and html tags escaped. I have yet to figure out why I need bothonReady
andonLoad
.... but at the moment it's the only way it worked for me. I will update if I find anything new.It was successful.
使用 remoteCommand 的 OndrejM 第二个选项,但在 outputPanel 内通过添加
deferred="true"< 配置为在页面加载后加载/code> 和
deferredMode="load"
:Using OndrejM second option of a remoteCommand but inside an outputPanel configured to load after the page load by adding
deferred="true"
anddeferredMode="load"
:在您的
中包含(在页面底部)jQuery 库,然后使用$(document)。像往常一样准备好:
Include (at the bottom of your page) jQuery library within your
<ui:define name="content">
and then use$(document).ready
as usual: