prettify

baseline

Javascript i18n (internationalization)

The users of a web app should be able to select the interface language of preference. The default language is english and the developer has to give a list of the terms used along with the translations in all languages supported.
Each term can be
Each user selects a UI language that is stored in: ins.oUser.eUsrLang
Enumeration ins.enLng contains all the languages our system supports.
If a specified term is not supported, in the language that the user has chosen, then the english term is used.

Ajax C#

  • Visual Studio 2010/Solution Explorer
  • Solution.Add New Project.Visual C#.Web.ASP_NET Empty Web Application
  • Project.Add New Item.Visual C#.Generic Handler
aj.ashx
<%@ WebHandler Language="C#" Class="Aj" %>
using System;
using System.IO;
using System.Web;
public class Aj : IHttpHandler {
    public void ProcessRequest (HttpContext context) {
        Stream stIn = context.Request.InputStream;
        int nLen = (int)stIn.Length;
        byte[] aIn = new byte[nLen];
        stIn.Read(aIn, 0, nLen);
        System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
        string sData = enc.GetString(aIn);

        string[] aData = sData.Split();
        int nA = Convert.ToInt32(aData[2]);
        int nB = Convert.ToInt32(aData[3]);
        int nResult = nA + nB;  
        context.Response.CacheControl = "no-cache";
        context.Response.ContentType = "application/javascript";
        context.Response.Write("["+nResult.ToString()+"]");
    }
     public bool IsReusable {get {return false;}}
}

Ajax Javascript

How to call an ajax service from a web page using
  • Javascript
  • XMLHttpRequest
  • POST
The server side implementation using C# is in another post.

Javascript Stopwatch

I want to measure how long some processes are taking. So I wrote the Stopwatch class (click "Start", wait some seconds, click "Stop":
Class Stopwatch
"use strict";
ins.Stopwatch = function () {
    var dFr, dTo, that = this;
    this.nMilliseconds;
    this.nSeconds;
    this.nMinutes;
    this.nHours;
    this.nDays;
    var _bRun = false;
    this.bRun = function () { return _bRun; };
    this.Start = function () {
        _bRun = true;
        this.Reset();
        dFr = new Date();
    };
    this.Stop = function () {
        _bRun = false;
        if (!dFr) {
            return Reset();
        } else {
            dTo = new Date();
            return Calc();
        }
    };
    this.Reset = function () {
        dFr = dTo = null;
        this.nMilliseconds =
        this.nSeconds =
        this.nMinutes =
        this.nHours =
        this.nDays = 0;
        return 0;
    };
    this.toString = function (sFormat) {
        if (typeof sFormat !== "string") sFormat = "m:s.f";
        return sFormat.replace(/d/g, this.nDays.toString()).
            replace(/h/g, this.nHours.toString().PadLeft(2, "0")).
            replace(/m/g, this.nMinutes.toString().PadLeft(2, "0")).
            replace(/s/g, this.nSeconds.toString().PadLeft(2, "0")).
            replace(/f/g, this.nMilliseconds.toString().PadLeft(3,"0"));
    };
    var Calc = function () {
        if (dFr && dTo && dFr.getTime() < dTo.getTime()) {
            var nTime = dTo.getTime() - dFr.getTime()
            that.nMilliseconds = Math.round(nTime);

            that.nSeconds = Math.floor(that.nMilliseconds / 1000);
            that.nMilliseconds -= that.nSeconds * 1000;

            that.nMinutes = Math.floor(that.nSeconds / 60);
            that.nSeconds -= that.nMinutes * 60;

            that.nHours = Math.floor(that.nMinutes / 60);
            that.nMinutes -= that.nHours * 60;

            that.nDays = Math.floor(that.nHours / 24);
            that.nHours -= that.nDays * 24;
            return nTime;
        } else {
            return that.Reset();
        }
    };
    this.Reset();
};
if (typeof String.prototype.PadLeft != "function") {
    String.prototype.PadLeft = function (nLen, sPad) {
        if (typeof sPad !== "string") sPad = " ";
        if (typeof nLen !== "number") nLen = 2;
        var sResult = this;
        while (sResult.length < nLen) sResult = sPad + sResult;
        return sResult;
    };
}
Usage:
    var oSW = new i.Stopwatch();
    var oBtn, oDiv;
    window.onload = function () {
        oBtn = document.getElementById("btnFr");
        oDiv = document.getElementById("divResults");
        oBtn.onclick = ToggleSW;
    };
    var ToggleSW = function () {
        oBtn.disabled = true;
        if (oSW.bRun()) {
            oSW.Stop();
        } else {
            oSW.Start();
        }
        oBtn.innerHTML = oSW.bRun() ? "Stop" : "Start";
        oDiv.innerHTML = oSW.bRun() ? "" : oSW.toString("m:s:f");
        oBtn.removeAttribute("disabled");
    };