sexta-feira, 2 de janeiro de 2009

javascript: get URL parameter

To get a URL parameter using javascript, we can use the following function:

function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}

As explained here in netlobo.com

quarta-feira, 31 de dezembro de 2008

java: stacktrace with no line number


When getting a Java log exception with no line number and instead a message saying "Unkown source", just like you can see bellow:

...
at com.vgn.cgd.webApp.actions.pci.impl.ppcLoadAction.doExecute(Unknown Source)
...

It may be caused by Ant that by default omits the debug information. If it's the case you can easily solve this by add 2 attributes to your ant target javac:

....
<javac ... debug="on" debuglevel="lines,vars,source">
....


And there you go, the line number of the exception will appear in your log.

terça-feira, 30 de dezembro de 2008

java: log Stack Trace of exception

We can log the stacktrace of a exception by using the following function to build the String:

// function to compose the stacktrace of a exception to a String
public static String getStackTrace(Throwable aThrowable) {
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
aThrowable.printStackTrace(printWriter);
return result.toString();
}

java native array initialization

String[] s = new String[100];   // default values: null
boolean[] b = new boolean[4]; // default values: false
int[] i = new int[10][10]; // default values: 0

Get cookie value using javascript

We can use the following function to get a cookie value from dom document.cookie using javascript:

function getCookie( check_name ) {
var a_all_cookies = document.cookie.split( ';' );
var a_temp_cookie = '';
var cookie_name = '';
var cookie_value = '';
var b_cookie_found = false;

for ( i = 0; i <>
a_temp_cookie = a_all_cookies[i].split( '=' );
cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
if ( cookie_name == check_name ){
b_cookie_found = true;
if ( a_temp_cookie.length > 1 ){
cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
}
return cookie_value;
break;
}
a_temp_cookie = null;
cookie_name = '';
}
if ( !b_cookie_found ){
return null;
}
}//getCookie

IE8 window.open doesn't inherit session cookies of parent window

Beware using cookies and the new IE8 browser, as there are still some problems microsoft didn't handle.
A window.open does not inherit the parent window cookies. more here on microsoft.com

You may want to access the parent window cookies issuing:
window.opener.document.cookie