Blog posts

  • How to set this when calling a javascript function

    February 04, 2014 | Tags: Javascript, jQuery, Snippet
    Ever wondered how the this entity was set in a function like for instance a jQuery event handler? Or maybe even needed to invoke that event handler from code passing the object that would be accessed through this within the function? The secret is to use the call-function that is available on every function in JavaScript.

    If you for instance have an event handler setup in jQuery like the one below:
    function buttonClickHandler(e) {

    ..that you would like to invoke from code and passing the object that would be accessed through this within the function, using the call-function on your event handler function will do the trick:
    var btn = $('#button1');;

    The syntax of the call-function is: call(thisArg[, arg1[, arg2[, ...]]]). The first parameter you pass is what will be accessible through the this entity. Then follows optional parameters that will be passed as parameters to the function being called in the given order.

    If you which to pass an object as this to a function that takes two parameters (lets say showName(firstName, surName)) you would do it like this:
    function showName(firstName, surName) {
    $(this).val(firstName + ' ' + surName);

    var element = $('#element');, 'alice', 'cooper');

  • How to combine URLs/URIs in C#

    February 03, 2014 | Tags: C#, Snippet
    The file system have the Path.Combine method to combine paths but how to combine a base URL with an absolute or relative URL/URI?

    The answer is to use the System.Uri-constructor to combine the URL:

    public static Uri CombineUri(string baseUri, string relativeOrAbsoluteUri) {
    return new Uri(new Uri(baseUri), relativeOrAbsoluteUri);

    public static string CombineUriToString(string baseUri, string relativeOrAbsoluteUri) {
    return new Uri(new Uri(baseUri), relativeOrAbsoluteUri).ToString();


    // Results in ""
    var a = CombineUriToString("", "relative/path");

    // Results in ""
    var b = CombineUriToString("", "/absolute/path");

  • Convert JSON string to JavaScript objects safely

    February 03, 2014 | Tags: Javascript, JSON, jQuery
    If you have a JSON string that you want to convert into JavaScript object but doesn't want to use eval (which you should avoid due to the security implications), you can either use jQuery.parseJSON() or - if you're not using jQuery - use a dedicated JSON library such as JSON 3.
    // using jQuery
    var myObject = jQuery.parseJSON(myJSONString);

    // using JSON 3 (for polyfill in older browsers)
    var myObject2 = JSON.parse(myJSONString);

  • Passing arguments to another javascript function

    September 30, 2013 | Tags: Javascript, Snippet
    If you which to forward a call from one function to another passing all the parameters which were sent to the first function this can be done by using apply and passing arguments:
    function myFunction(args){
    myOtherFunction.apply(this, arguments);

    function myOtherFunction(fieldA, fieldB){
    alert(fieldA + " " + fieldB);


    Please note!The call to apply uses arguments and not args. This is intended and not a typo. The parameter args allows the function myFunction to receive arguments. The arguments object is an array of all the parameters sent to the function. If you don't want to specify which parameters myOtherFunction takes you can use arguments[0] etc in order to access the posted parameters.

  • C# test if object or type implements interface

    September 29, 2013 | Tags: C#, Snippet
    If you have a type or an instance you can easily check if they support a specific interface.

    To test if an object implements a certain interface:
    if(myObject is IMyInterface) {
    // object myObject implements IMyInterface

    To test if a type implements a certain interface:
    if(typeof(IMyInterface).IsAssignableFrom(typeof(MyType))) {
    // type MyType implements IMyInterface

    If you got a generic object and want to do a cast as well as a check if the interface you cast to is implemented the code is:

    var myCastedObject = myObject as IMyInterface;

    if(myCastedObject != null) {
    // object myObject implements IMyInterface