13 December 2009

submit form when Enter button is pressed

Use onkeypress attribute of all the elements in the current form (for which you intend to exploit Enter button’s hit to execute some action) by assigning a javascript method as shown below:

onkeypress="return submitEnter(Id_of_button_to_click_on_enter, event);"


where submitEnter method is as defined below:

function submitEnter(commandId,e) {

var keycode;

if (window.event)

keycode = window.event.keyCode;

else if (e)

keycode = e.which;

else

return true;

if (keycode == 13) {

document.getElementById(commandId).click();

return false;

} else

return true;

}

In my case, the elements of the form were simply fields, like



'loginForm:changepwdbtn’ – is the id of the button to be clicked when user hits ‘Enter’.

As shown above, call submitEnter() on ‘onkeypress’ of all the elements where you want to use Enter button to perform some action.

Same logic applies to click a command link on Enter, only change is in the submitEnter method.

Instead of

document.getElementById(commandId).click();

use

clickLink(commandId);


where definition of clickLink() is as below:

function clickLink(linkId) {

var fireOnThis = document.getElementById(linkId)

if (document.createEvent) {

var evObj = document.createEvent('MouseEvents')

evObj.initEvent( 'click', true, false )

fireOnThis.dispatchEvent(evObj)

} else if (document.createEventObject) {

fireOnThis.fireEvent('onclick')

}

}

P.S: What I observed (may not be true for all) is that, first in the first form used in a page gets associated with Enter button by default. No special logic/method as explained above is required for such cases. But when you want Enter button to click any button other than the first one, then this post might be useful

And also http://wiki.apache.org/myfaces/SubmitPageOnValueChange should be very useful.


Hope this helps!

05 December 2009

get Internet Explorer version through javascript

Effective way to get Internet Explorer version through Javascript is

function getIEVersion() {
// Returns the version of Internet Explorer or a -1
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer')
{
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat( RegExp.$1 );
}
return rv;
}

Hope this helps!

11 August 2009

Work...work...work...

Oh! I just forgot this space...really was very busy killing time consistently at my office with jsf.

Work...work...work...

19 January 2009

The value of attribute "..." associated with an element type "..." must not contain the '<' character.

A parameter was declared under the xsl as


I was trying to populate an attribute of a node in xsl, by using the above declared parameter value as


But this yielded the following error.

The value of attribute "attrName" associated with an element type "parentnode" must not contain the '<' character.


After trying lot of combinations/ways, I found out a way to do it and is

That is simply access the xsl:param as you access a xsl:variable.

Another way of doing it is first declare a variable whose value is the xsl:param value, then use this variable in the same way as did it in the above case


As you can see the second approach is not recommended over the first one.

Hope this helps!