including a string in the url for a tapestry event

In tapestry5, events are handled by calling a page. You can pass so-called “context” parameters in such calls and these are passed in the URL (after conversion to string, thanks to the translators and coercers).

When you want to manually modify or add such parameters from the client-side, you need some javascript code which does the url encoding in the same way as tapestry does it internally. The following code snippet shows a way this can be done.

/**
 * Url encode a string to ensure it can be passed to tapestry5
 * @param string
 */
function eqUrlEnc( string )
{
    string = string.replace(/\r\n/g,"\n");
    var res = "";
    for (var n = 0; n < string.length; n++)
    {
        var c = string.charCodeAt( n );
        if ( '$' == string.charAt( n ) )
        {
            res += '$$';
        }
        else if ( eqInRange( c, "AZ" ) || eqInRange( c, "az" ) || eqInRange( c, "09" ) || eqInRange( c, ".." ) )
        {
            res += string.charAt( n )
        }
        else
        {
            var tmp = c.toString(16);
            while ( tmp.length < 4 ) tmp = "0" + tmp;
            res += '$' + tmp;
        }
    }
    return res;
}

function eqInRange( code, range )
{
    return code >= range.charCodeAt( 0 ) &&  code <= range.charCodeAt( 1 );
}

47 Comments

  1. Oliver P. says:

    this was exactly what I was looking for! Works like a charme!!

    Thank you for that post!!!
    😀

Leave a Reply

Your email address will not be published. Required fields are marked *

question razz sad evil exclaim smile redface biggrin surprised eek confused cool lol mad twisted rolleyes wink idea arrow neutral cry mrgreen

*