1

I am trying to fire an JSF backing bean function from JavaScript when the page is being unload. It is working fine in IE and chrome but not in Firefox.

This is how the onunload event get trigger in XHTML:

<h:body onunload="onPageUnload()">
  <h:form id="theForm">
    <p id="hiddenButton" style="display:none">
      <h:commandLink id="hiddenCloseEvent" styleClass="button" action="#{theBean.doHiddenCloseEvent}">
        <f:param name="action" value="CLOSE"/>
      </h:commandLink>
    </p>
  ...
  </form>
</h:body>

When the unload event is receive, the follow JavaScript will be execute. There are 2 versions, one is for IE, and the other one is for Google Chrome.

IE version:

function onPageUnload() {
  if( detailPage == "false" ) {
    document.getElementById('theForm:hiddenCloseEvent').click();
  }
}

Google Chrome version:

window.onbeforeunload = function(e) {
  onPageUnload();
};

When the onPageUnload() is execute, it trigger the hidden input button JSF component click event, and then fire the JSF backing bean, theBean, function theBean.doHiddenCloseEvent to do some processing. Now the situation is the JSF function is get executed in IE and Google Chrome version but not being trigger in Firefox, may I know how this problem could be solve?

UPDATE

I have modified the code like below:

function onPageUnload() {
  if( detailPage == "false" ) {
    alert('before');
    document.getElementById('theForm:hiddenCloseEvent').click();
    alert('after');
  }
}

From the code above I found out that the before get prompt but the after doesn't prompt. This test case is verify whether the component isn't visible in the web browser.

huahsin68
  • 6,819
  • 20
  • 79
  • 113
  • Could you please post (if any) Java Script errors in Error Console in Firefox? – Vikas V Nov 02 '12 at 05:04
  • there is no error on JavaScript site because I have tested it with `alert()`. The alert is prompt, but just the click event not functioning. – huahsin68 Nov 02 '12 at 05:47

1 Answers1

0

I believe this is a special issue with firefox. It's recommended you use onbeforeunload to cater for FF.

See the Firefox forum and also here. onbeforeunload is not supported by all browsers though

You could use both events to cover both bases.

kolossus
  • 20,559
  • 3
  • 52
  • 104
  • I'm just curious, can I have two unload attributes on a same time ``? – huahsin68 Nov 02 '12 at 07:16
  • Yes you can, in theory, they're two separately (named) events. See http://stackoverflow.com/a/3775590/1530938. – kolossus Nov 02 '12 at 07:32
  • If the 2 can sit together, but the onbeforeunload seems not working. I think now the current issue is the `alert('before')` is prompt and the `alert('after')` isn't prompt. This indicate that the unload event has been trigger, I am curious the root cause is `document.getElementById('theForm:hiddenCloseEvent').click();` It could be the component wasn't thus the click event not able to trigger. – huahsin68 Nov 03 '12 at 01:35