Posts Tagged ‘Javascript’ provides JS libraries for animation, drag and drop, Ajax controls, DOM utilities, and unit testing. Good to know.

<script src=”; language=”javascript”></script>
<script language=”javascript”>
$(document).ready(function() {
window.setTimeout(function() {$(‘#box’).removeClass(“box1”).addClass(“boxCls”);}, 5000);


<style type=”text/css”>
.boxCls {
position: absolute;
width: 200px;
height: 200px;
top: 0;
left: 200px;
background-color: white;
border: 10px solid black;

.box1 {
position: absolute;
width: 50px;
height: 50px;
top: 0;
left: 0;
background-color: yellow;
border: 2px solid red;
<div id=”box” class=”box1″>Box</div>

In IE/Firefox we can use window.showModalDialog to show modal popup window. 
Use window.returnValue in the popup window to pass the selections to parent window.
Main Page Code ...
<script type="text/javascript">
    function ModalPopup() {
        var retValue = window.showModalDialog('popup.htm', 'height=200,width=150');
    <a href="#" onclick='javascript:ModalPopup()'>Click</a>
Popup Window Code ...
        function dosubmit() {
            window.returnValue = document.getElementById("txtName").value;
    <a href='#' onclick='javascript:dosubmit()'>Close</a>
    <input value="hi" type="text" id="txtName" />
But this showModalDialog has disadvantages like it will not work in Chrome and some other browsers and also test automation of UI using Selenium software will not.
Refer the below links for solution on Selenium issue ...

JSON to C# object to JSON

Posted: September 8, 2011 in C#, Javascript, JQuery, JSON
Tags: , , ,

public class SearchFilter
public string SearchCol { get; set; }
public string SearchOp { get; set; }
public string SearchText { get; set; }
public int PageNo { get; set; }
using System;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
public class JSONHelper
public static T Deserialise<T>(string json)
T obj = Activator.CreateInstance<T>();
using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
obj = (T)serializer.ReadObject(ms);
return obj;

public static string Serialize<T>(T obj)
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
using (MemoryStream ms = new MemoryStream())
serializer.WriteObject(ms, obj);
return Encoding.Default.GetString(ms.ToArray());
Usage –

SearchFilter lSearchFilter = JSONHelper.Deserialise<SearchFilter>(“{‘SearchCol’:’Col1′,’SearchOp’:’contains’,’SearchText’:’hi’}”);

function ToJSONKeyValueFormat(pKey, pValue, pIsString) {
return ((pIsString)? (“\”” + pKey + “\”:\”” + pValue + “\””) : (“\”” + pKey + “\”:” + pValue));
function ToJSONFormat() {
var lJSONString = “”;
for (var i = 0; i < arguments.length; i++) {
lJSONString = ((i === 0)? “{” + arguments[i] : lJSONString = lJSONString + “,” + arguments[i]);
return ((lJSONString !== “”) ? lJSONString + “}” : “”);


ToJSONFormat(ToJSONKeyValueFormat(“Key1”, “Val1”, true), ToJSONKeyValueFormat(“Key2”, 2, false))



String.prototype.format = function() {
    var args = arguments;
    return this.replace(/{(\d+)}/g, function(match, number) {
        return typeof (args[number] != 'undefined') ? args[number] : '{' + number + '}';
Extending javascript global object is not a good idea. This can be defined as a utility funciton with in your application's global js object/file.

Extending  Reference -

Do you know how to use Firebug’s console effectively?

Execute jQuery/javascript statement at the console, with your page loaded in the browser, and you will instantly see the array of matching elements that are returned. If that result set isn’t correct, press the up-arrow key to retrieve the last command entered, refine the statement, and test it again

It works in conjunction with Firebug’s debugger. When execution is paused, switching to the console tab allows you to interrogate and manipulate the state of the DOM as it was at the time execution was halted. As you step through JavaScript code in the debugger, the execution context of the console remains in sync with the debugger’s.

Check your javascript code quality using

Define all your functions and variables in the private scope and return an anonymous object at the end of the module with pointers to the private variables and functions you want to reveal as public. Check more @

Ex: Singleton pattern using Revealing Module Pattern

var singleton = function () {
var privateVar = 10;
function privateMethod1() {
return privateVar;
function SetVal1(val) {
privateVar = val;
function GetVal1() {
return privateVar;
function GetVal2() {
return privateMethod1();
return {
SetVal1: SetVal1,
GetVal1: GetVal1,
GetVal2: GetVal2
} ();

var obj1 = singleton;
var obj2 = singleton;