dracoblue.net

UrlEncode and Decode in JavaScript

While using the default JavaScript functions escape/unescape to (un)escape strings for url parameters, you'll run into issues, since JavaScript threats + as valid character and so on.

At PHPBuilder-Forums I found an

implementation, which does not work, since it only replaces the first + and leaves all other unchanged. That is very much wrong.

A fixed, and working implementation for urlencode can be found here:

function urlencode(str) {
 str = escape(str);
 str = str.replace(/\\+/g,'%2B');
 str = str.replace(/\\%20/g,'+');
 str = str.replace(/\\*/g,'%2A');
 str = str.replace(/\\//g,'%2F');
 str = str.replace(/\\@/g,'%40');
 return str;
}

In javascript, open source by DracoBlue @ 11 Jun 2008 | 96 Words

JavaScript new Exception(name,message)

Today I noticed that JavaScript doesn't provide an easy way to make/raise custom exceptions? while still providing an useful stacktrace.

Here is a code snippet used for the

GTA:T JavaScript interface to find the fitting stacktrace for a custom JavaScript Exception.

function Exception(name,msg) {
    try {
        throw new Error("")
    } catch (e) {
        e.stack = e.stack.split("@"+e.fileName+":").join(":");
        full_stack = e.stack.split("\\n");
        stack = [];
        stack[0] = "Exception: "+name+"(\""+msg+"\")"
        for (var i=2;i<full_stack.length-3;i++) {
            entry = full_stack[i];
            entry_detailed = entry.split(":");
            entry_detailed[1] = entry_detailed[1] - 4; // THIS is to
            // mark, that we'll "move" the source 4 lines higher,
            // ... because it's eval code executed. Remove that for
            // clear values.
            if (i==2) lineNumber = entry_detailed[1];
            stack[i] = entry_detailed.join(":");
        }
        return {
            name:name,
            message:msg,
            stack:stack.join("\\n"),
            lineNumber:lineNumber
        };
    }
}

You may use it that way:

try {
    function a() {
        throw new Exception("MyException","No permission!")
    }
    function b() {
        a()
    }
    b()
} catch (e) {
    alert(e.name+": "+e.message);
}
// Tested in Firefox
/* e:
Object {
  [name] => MyException
  [message] => No permission!
  [stack] => Exception: MyException("No permission!")
             a():3
             b():6
             execute():10
  [lineNumber] => 3
}*/

In html, javascript, open source by DracoBlue @ 05 Jun 2008 | 194 Words

Implode an Array in Java

Its quite simple to implement the function, you may know from

php, which concats all elements from an array as string and puts a seperator between those.

The following code snippet, takes all elements from inputArray (e.g. 1 2 and 3) and concat them to "1,2,3" and saves this into AsImplodedString.

String AsImplodedString;
if (inputArray.length==0) {
    AsImplodedString = "";
} else {
    StringBuffer sb = new StringBuffer();
    sb.append(inputArray[0]);
    for (int i=1;i<inputArray.length;i++) {
        sb.append(",");
        sb.append(inputArray[i]);
    }
    AsImplodedString = sb.toString();
}

This implementation uses a StringBuffer to be faster with big arrays.

In java, open source by DracoBlue @ 11 Apr 2008 | 97 Words

Hal won't upgrade when updating from Etch to Lenny

While I performed apt-get dist-upgrade on my debian machine, to update from Etch to Lenny I had the issue, that the HAL-package was not able to be upgraded (which caused some other parts of debian not to be able to be upgraded).

I found out how to fix that problem, since I noticed that hal wasn't started properly.

If HAL hangs on upgrade, press CTRL+C to stop apt-get. Then type:

/etc/ini.d/dbus restart

And after that do the following again:

apt-get dist-upgrade

Now everything should work fine.

In articles, linux by DracoBlue @ 18 Mar 2008 | 96 Words

Avoid session expiration in Webapplications (e.g. PHP)

Some of you may have run into the problem that the default session timeout (about 15 minutes) isn't always useful if your users have to fill a large form with lots of text.

Often you get after those: 'Session timed out' and after login the user has to type all again. To fix that, you can wait for the login and refill the form with the saved data. That is not very handy and quite difficult to implement.

A very handy and fast fix for this session timeout is, that you query the server every minute once to keep the session alive without requiring the user to reload the page. I found out that that soloution is used even in SMF, so it seems to be quite common.

So just add the following lines to an extra page or the start of your index-file.

session_start(); if (isset($_GET['sessionfix']))Â? exit;

And now add the magic javascript, which updates that in the template or html file.

That's it!

In articles, open source, php by DracoBlue @ 27 Feb 2008 | 167 Words

Page 34 - Page 35 - Page 36