页面加载后如何执行JavaScript?

发布于 2024-12-28 16:40:46 字数 1041 浏览 0 评论 0原文

我想在页面加载后执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

追星践月 2025-01-04 16:40:46

按如下方式使用 JQuery:

<script>
    jQuery(document).ready(function() {
        PathFinder.findAndGo();
    });
</script>

更新:

它必须位于 内。

Use JQuery as follows:

<script>
    jQuery(document).ready(function() {
        PathFinder.findAndGo();
    });
</script>

Update:

It has to be within <ui:define name="content">.

难得心□动 2025-01-04 16:40:46

当我想在 Primefaces 中加载页面后执行 JS 函数时,有两种方法对我有用:

  • 要么使用 jQuery(因为 Primefaces 已经使用它),最好的解决方案是嵌套 document.ready 两次,以便我的 JS代码在所有 Primefaces 代码之后执行:
    • jQuery(文档).ready(function () {
      jQuery(文档).ready(函数() {
      // 在 document.ready 中执行两次,在 Primefaces 回调后执行
      路径查找器.findAndGo();
      });
      });
  • 或使用带有自动运行功能的 p:remoteCommand,并在其上放置 oncomplete JS 回调
    • 这种方式表单通过AJAX提交到服务器,但服务器端没有执行监听器,之后执行JS回调

There are 2 ways that work for me, when I want to execute a JS function after page load in Primefaces:

  • either use jQuery (as it is already used by Primefaces), best solution is to nest document.ready twice, so that my JS code is executed after all Primefaces code:
    • jQuery(document).ready(function () {
      jQuery(document).ready(function () {
      // twice in document.ready to execute after Primefaces callbacks
      PathFinder.findAndGo();
      });
      });
  • or use p:remoteCommand with autorun, and place oncomplete JS callback on it
    • this way form is submitted by AJAX to server but no listener executed on server side, a JS callback is executed afterwards
    • <p:remoteCommand oncomplete=" PathFinder.findAndGo(); " autoRun="true"/>
ま柒月 2025-01-04 16:40:46
window.onload = function () {
  // code to execute here
}
window.onload = function () {
  // code to execute here
}
给妤﹃绝世温柔 2025-01-04 16:40:46

对于 primefaces,我成功地使用了以下内容:

  1. 我已在 中加载了包含我需要 onLoad 的函数的 JS 文件:

    <前><代码>< h:outputScript库=“javascript”名称=“nameOfMyFile.js”目标=“head”>

  2. 我使用了以下内容来运行 JS 函数我需要来自“nameOfMyFile.js”:

    <前><代码>< h:body onload="nameOfMyFunction()" >

  3. 请注意,我的 js 文件底部还包含以下命令:

    $(document).ready(nameOfMyFunction());
    

第三点用于运行函数 onReady。这完全是一个实验,看看我是否可以将 HTML 添加到计划事件中......因为传递的字符串被解析并且 html 标签被转义。我还没有弄清楚为什么我需要 onReadyonLoad....但目前这是对我有用的唯一方法。如果我发现任何新内容,我会更新。
成功了。

For primefaces I have managed to successfully use the following:

  1. I have loaded in <head> the JS file containing the functions I needed onLoad by using:

    < h:outputScript library="javascript" name="nameOfMyFile.js" target="head">
    
  2. I have used the following in order to run the JS functions I needed from "nameOfMyFile.js":

    < h:body onload="nameOfMyFunction()" >
    
  3. Please note that my js file also contained the following command at the bottom:

    $(document).ready(nameOfMyFunction());
    

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 both onReady and onLoad.... but at the moment it's the only way it worked for me. I will update if I find anything new.
It was successful.

像你 2025-01-04 16:40:46

使用 remoteCommandOndrejM 第二个选项,但在 outputPanel 内通过添加 deferred="true"< 配置为在页面加载后加载/code> 和 deferredMode="load"

<p:outputPanel deferred="true" deferredMode="load" rendered="#{...}"> 
     <p:remoteCommand oncomplete="PathFinder.findAndGo();" autoRun="true" />
</p:outputPanel>

Using OndrejM second option of a remoteCommand but inside an outputPanel configured to load after the page load by adding deferred="true" and deferredMode="load":

<p:outputPanel deferred="true" deferredMode="load" rendered="#{...}"> 
     <p:remoteCommand oncomplete="PathFinder.findAndGo();" autoRun="true" />
</p:outputPanel>
記憶穿過時間隧道 2025-01-04 16:40:46

在您的 中包含(在页面底部)jQuery 库,然后使用 $(document)。像往常一样准备好:

<ui:define name="content">
...
<!-- jQuery -->
<h:outputScript name="vendor/jquery/jquery.min.js"/>
<script>
  $(document).ready(function(){
      alert(1);
  });
</script>
</ui:define>

Include (at the bottom of your page) jQuery library within your <ui:define name="content"> and then use $(document).ready as usual:

<ui:define name="content">
...
<!-- jQuery -->
<h:outputScript name="vendor/jquery/jquery.min.js"/>
<script>
  $(document).ready(function(){
      alert(1);
  });
</script>
</ui:define>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文