JSF 2.0 javascript onload/oncomplete

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5570180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:38:33  来源:igfitidea点击:

JSF 2.0 javascript onload/oncomplete

javascriptjsfonloadmyfaces

提问by Matt

I'm working with Myfaces 2.0 after a long project using MyFaces 1.1 + Tomahawk + Ajax4JSF. 2.0 is definitely a huge improvement. Although I can't seem to get an onload javascript to work. I stuck this in my head:

在使用 MyFaces 1.1 + Tomahawk + ​​Ajax4JSF 进行了一个长期项目后,我正在使用 Myfaces 2.0。2.0绝对是一个巨大的进步。虽然我似乎无法让 onload javascript 工作。我把这个记在我的脑海里:

<script type="text/javascript">
    window.onload = function () {
        var fileDownload = document.getElementById('userManagementForm:fileDownload');
        if (fileDownload.value == 'true') {
            fileDownload.value = false;
            window.open('Download');
        }
    }                   
    </script>

But it doesn't execute. I looked a bit at the source myfacecs JS and I noticed they do some stuff with the onload. Didn't get too much into the details but it seems to block my attempt at overriding the onload event. Is there another way around this, maybe a direct call to the JSF library?

但它不执行。我查看了 myfacecs JS 的源代码,我注意到他们对 onload 做了一些事情。没有深入了解细节,但它似乎阻止了我覆盖 onload 事件的尝试。有没有其他方法可以解决这个问题,也许是直接调用 JSF 库?

Another question, sort of on the same topic. Ajax4JSF had an attribute "oncomplete" which would let you run some Javascript after the Ajax request completed. Is there an equivalent in MyFaces 2.0 with the f:ajax? Come to think of it I haven't tried event="oncomplete", maybe it'll work.

另一个问题,有点关于同一主题。Ajax4JSF 有一个属性“oncomplete”,它可以让您在 Ajax 请求完成后运行一些 Javascript。MyFaces 2.0 中是否有 f:ajax 的等价物?想想我还没有尝试过 event="oncomplete",也许它会起作用。

回答by Matt

Okay so I figured it out.

好的,所以我想通了。

Loading the script at the top of page later gets overridden

稍后在页面顶部加载脚本会被覆盖

If I load the script at the end of the page it overrides the onload, but it also breaks the JSF mechanism. So the better solution was to user JSF's onload thing and put the script at the end of the page. After digging through the JSF source a bit I found the function.

如果我在页面末尾加载脚本,它会覆盖 onload,但也会破坏 JSF 机制。所以更好的解决方案是用户 JSF 的 onload 东西并将脚本放在页面的末尾。在对 JSF 源代码进行了一番挖掘后,我找到了该函数。

<script type="text/javascript">
        myfaces._impl.core._Runtime.addOnLoad(window, checkDownloadFile);
</script>

And of course I had to define checkDownloadFile which does what I had in the original post.

当然,我必须定义 checkDownloadFile 来完成我在原始帖子中所做的工作。

I also figured out for the oncomplete. The f:ajax tag has an onevent attribute which gets called 3 times. Once before execution and twice after. I believe once immediately after completing the request, then again after updating the page components. The callback function takes an event parameter which has a status. So in your callback function you check the status to decide whether or not to do something. The statuses are begin, complete, and success. So for my case I have:

我也想出了oncomplete。f:ajax 标签有一个 onevent 属性,它被调用 3 次。执行前一次,执行后两次。我相信在完成请求后立即,然后在更新页面组件后再次相信。回调函数采用具有状态的事件参数。所以在你的回调函数中,你检查状态来决定是否做某事。状态为开始、完成和成功。所以对于我的情况,我有:

<f:ajax execute="userManagementForm" event="click" onevent="myEvent" render="userManagementForm:results userManagementForm:workarea userManagementForm:fileDownload" />

And then the script:

然后是脚本:

    function myEvent(e) {
        if (e.status == 'success') {
            checkDownloadFile();
        }
    }