I'm trying to simulate onclick event in a h:commandButton when I close my browser.
Here is my code:
<body onBeforeUnload='closingBrowser()'>
function closingBrowser() {
document.getElementById('main:commandButtonHidden').onclick();
return false;
}
This javascript function call the function associated with my button which has this definition:
<h:commandButton id="commandButtonHidden"
value="checkPasswords().Login"
onclick="javascript:checkPasswords();" actionListen开发者_Go百科er="#{init.closingBrowser}" />
and checkPasswords():
function checkPasswords() {
alert("checkPasswords");
return true;
}
This function has nothing interesting because what I want is the function in my actionListener.
This works perfect in IE and FF, but not in chrome or Opera.
The alert always is fired in all browsers, but the actionListener NO, just in IE and FF, and it has no sense.
Somebody knows anything about this.
Thanks
In case anyone bumps into this looking for a framework agnostic way to fire any HTML and Mouse event, have a look here: How to simulate a mouse click using JavaScript?
Wild guessing: try firing a click event, instead of accessing the onclick
callback.
document.getElementById('main:commandButtonHidden').click();
I found this while searching for the solution to this problem. I'm hoping its not too late for you but I did:
(with jQuery)
var element = jQuery("#" + elementid);
if (element.length > 0 && element[0].click == null)
{
element[0].click = function ()
{
var mouseEvent = document.createEvent("MouseEvent");
mouseEvent.initMouseEvent("click", true, true, window, 0,
element.offset().left + window.screenX, element.offset().top + window.screenY,
element.offset().left, element.offset().top, false, false, false, 0, null);
element[0].dispatchEvent(mouseEvent);
};
}
if you don't want to use jQuery (and you don't need client/screen x/y co-ords)
simply try:
var mouseEvent = document.createEvent("MouseEvent");
mouseEvent.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, 0, null);
var element = document.getElementById('main:commandButtonHidden');
element.dispatchEvent(mouseEvent);
from: https://developer.mozilla.org/en/DOM/document.createEvent
精彩评论