Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top