最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JSF 2.0 javascript onloadoncomplete - Stack Overflow

programmeradmin0浏览0评论

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:

<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?

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

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:

<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?

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

Share Improve this question asked Apr 6, 2011 at 17:01 MattMatt 511 gold badge1 silver badge4 bronze badges 1
  • as HTML do you get any other method which is hooked up with onLoad() ?> – Jigar Joshi Commented Apr 6, 2011 at 17:07
Add a ment  | 

1 Answer 1

Reset to default 4

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.

<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.

I also figured out for the onplete. The f:ajax tag has an onevent attribute which gets called 3 times. Once before execution and twice after. I believe once immediately after pleting the request, then again after updating the page ponents. 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, plete, and success. So for my case I have:

<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();
        }
    }
发布评论

评论列表(0)

  1. 暂无评论