Monday 22 September 2008

Javascript: Read Selected Value of a Radio Group

When you first approach reading the value of a radio group input, you may be fooled into assuming it would be similar to reading a text input, IE "document.myForm.myInput.value", however radio groups are not so straight forward.

To get the value you need to loop through the radio group elements to find which one is checked, then get the value for that element.

This example contains the getRadioValue function which can be added to any page, just pass the object reference of the radio group, as the parameter.

It's also an example of how to add a radio to your form.

<html>

<head>
<script>
function getRadioValue(radioObj)
{
var radioValue = 'undefined';
for (i=0; i<radioObj.length; i++)
{
if(radioObj[i].checked==true)
{
radioValue = radioObj[i].value;
break;
}
}
return radioValue;
}
</script>
</head>

<body>

<h1>Get Radio Group Value</h1>

<form name="myForm">

<input type="radio" name="myRadioGroup" value="R">Red</input> <br/>
<input type="radio" name="myRadioGroup" value="G">Green</input> <br/>
<input type="radio" name="myRadioGroup" value="B">Blue</input> <br/>
<br/>
<input type="button" onclick="alert(getRadioValue(document.myForm.myRadioGroup));" value="Test"/>

</form>

</body>

</html>

Value Got.

Wednesday 17 September 2008

Confirm Browser Window Close with Javascript onBeforeUnload

If you have a web page that if closed prematurly could lose users work or data, then you may want to add a prompt to confirm the user wishes to close window. Rather than the default browser behaviour of just closing or opening a new page over your page in the same window..

To to this you can add an onbeforeunload Javascript function. Please note that this doesn't work with some older browsers.

This is a basic example that should display a message when the window or tab is closed.

<html>
<head>

<script type="text/javascript">

window.onbeforeunload = function() {
return "Are you sure you wish to close this page?";
}

</script>

</head>
<body>
<h1>On Before Unload Test</h1>
</body>
</html>

This should help prevent any lost work.

Done.

Friday 12 September 2008

Oracle DBA Script for Checking Tablespace Used/Free Space

This select statement returns tablespace information including total size, used Mb, free Mb and the number of data files used by the tablespace.

SELECT dt.tablespace_name,
dt.contents,
COUNT(DISTINCT ddf.file_id) dataFileCount,
DECODE(SUM(ddf.maxBytes),0,SUM(ddf.bytes)/(1024*1024),SUM(ddf.maxBytes)/(1024*1024)) MBTotal,
DECODE(SUM(ddf.maxBytes),0,(SUM(ddf.bytes)/(1024*1024))-(ROUND(SUM(dfs.bytes)/(1024*1024))),SUM(ddf.bytes)/(1024*1024)) MBUsed,
DECODE(SUM(ddf.maxBytes),0,ROUND(SUM(dfs.bytes)/(1024*1024)),(SUM(ddf.maxBytes)-SUM(ddf.bytes))/(1024*1024)) MBFree
FROM sys.dba_tablespaces dt,
sys.dba_data_files ddf,
( SELECT file_id,
SUM(bytes) bytes
FROM sys.dba_free_space
GROUP BY file_id ) dfs
WHERE dt.tablespace_name = ddf.tablespace_name
AND ddf.file_id = dfs.file_id(+)
GROUP BY dt.tablespace_name, dt.contents
ORDER BY 1;

Simple, but useful

Thursday 11 September 2008

JavaScript getElementsByClass Function and Example

Why is there no standard function in Javascript for getting elements by class? Who knows, however it's not the end of the World, as it's not too difficult to write one, or copy the one I've written here.

With this function an array of elements is returned which are of the class specified. There are two further optional parameters which enable you to filter the array by tag name or only those contained within a specified parent element.

function getElementsByClass(getClass,tag,node)
{

// Set optional defaults
if (tag == null)
tag = '*';
if (node == null)
node = document;

// Load constants
const allElements2 = document.getElementsByTagName('*');
const allElements = node.getElementsByTagName(tag);
const elements = new Array();
const pattern = new RegExp("(^|\\s)"+getClass+"(\\s|$)");

// Loop allElements
var e = 0;
for (var i=0; i<allElements.length; i++)
{
if (pattern.test(allElements[i].className) ) {
elements[e] = allElements[i];
e++;
}
}

// Return elemnts array
return elements;

}

Parameters:
  • getClass: the specified class to select. (Called getClass rather than class due to reserved words in IE)
  • tag: Optional parameter to filter the returned elements by tag.
  • node: Optional parameter to filter returned elements to child elements of this parent.

Use this test page to test the function and syntax for calling it.

<html>

<head>
<script>

function getElementsByClass(getClass,tag,node)
{

// Set optional defaults
if (tag == null)
tag = '*';
if (node == null)
node = document;

// Load constants
const allElements2 = document.getElementsByTagName('*');
const allElements = node.getElementsByTagName(tag);
const elements = new Array();
const pattern = new RegExp("(^|\\s)"+getClass+"(\\s|$)");

// Loop allElements
var e = 0;
for (var i=0; i<allElements.length; i++)
{
if (pattern.test(allElements[i].className) ) {
elements[e] = allElements[i];
e++;
}
}

// Return elemnts array
return elements;

}

function alertIdForClass(c,n,t)
{
var e;
if(n)
e = getElementsByClass(c,t,document.getElementById(n));
else
e = getElementsByClass(c,t);
for(var i=0; i<e.length; i++)
alert(e[i].id);
}

</script>
</head>

<body>

<h1>Get Element By Class Test</h1>

<div id="d1" class="c1">
<div id="d2" class="c1">
<span id="s1" class="c1"></span>
<span id="s2" class="c2"></span>
<span id="s3" class="c2"></span>
</div>
</div>

<pre>
&lt;div id="d1" class="c1">
&lt;div id="d2" class="c1">
&lt;span id="s1" class="c1">&lt;/span>
&lt;span id="s2" class="c2">&lt;/span>
&lt;span id="s3" class="c2">&lt;/span>
&lt;/div>
&lt;/div>
</pre>

<input type="button" value="Class 'c1'" onClick="alertIdForClass('c1');"/><br/><br/>
<input type="button" value="Class 'c1' within Element 'd1'" onClick="alertIdForClass('c1','d1');"/><br/><br/>
<input type="button" value="Class 'c1' within Element 'd1' With 'div' Tag" onClick="alertIdForClass('c1','d1','div');"/><br/><br/>
<input type="button" value="Class 'c2'" onClick="alertIdForClass('c2');"/><br/><br/>

</body>

</html>

ElementsByClass...Got

Thursday 4 September 2008

Java Unchecked Cast Warning

An annoyance with Java that I had resently was with the unchecked cast warning. I had placed a String List in the HttpSession, but when I ready it back I got the warning "warning: [unchecked] unchecked cast"

I knew no error would occur, as I was setting the attribute in the HttpSession in the same servlet and I didn't want loads of warnings to build up, as this can hide real warnings I need to action.

So, my original code looked like

private void foo(HttpServletRequest request) {
HttpSession session = request.getSession(true);
List myItems = (ArrayList)session.getAttribute("myItems");
for (String myItem : myItems) {
...

I found that to stop the error appearing you can suppress the warning, by adding the SupressWarnings command anywhere before the line that gives the warning.

private void foo(HttpServletRequest request) {
HttpSession session = request.getSession(true);
@SuppressWarnings("unchecked")
List myItems = (ArrayList)session.getAttribute("myItems");
for (String myItem : myItems) {
...

Putting it just before the warning line, allows unexpected warnings before to still appear. But what about afterwards? In my case it wasn't necessary as the function had no more casts, but

private void foo(HttpServletRequest request) {
HttpSession session = request.getSession(true);
List myItems = null;
{
@SuppressWarnings("unchecked")
List uncheckedList = (ArrayList)session.getAttribute("myItems");
myItems = uncheckedList;
}
for (String myItem : myItems) {
...

Placing the SuppressWarnings in brackets keeps the suppression inside, so any unexpected warnings afterwards will still appear.

I think it depends on your code as to if you need to go this far or just leave it as I did, with SuppressWarnings just before the warning line, or even place it at the top of the package.

Where is best... You Decide.