页面加载后如何执行 JavaScript? [英] How to execute JavaScript after page load?
问题描述
我想在页面加载后执行一个 JavaScript 函数.目前我有一个 commandButton,一切正常.但是如果用户不应该点击按钮会更舒服.
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.
我尝试过 f:event,但我没有监听器,我只有 JavaScript 函数.此外,body onload 对我不起作用,因为我只使用高级组件.
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>
JavaScript 函数 PathFinder.findAndGo 在我的 master.xhtml 中定义
The JavaScript function PathFinder.findAndGo is defined in my master.xhtml
推荐答案
使用JQuery如下:
Use JQuery as follows:
<script>
jQuery(document).ready(function() {
PathFinder.findAndGo();
});
</script>
更新:
它必须在
内.
这篇关于页面加载后如何执行 JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!