Make minimum use of setters and getters if possible. Such accessors look like .NET like kind of beautiful properties, but these create new more scopes for JavaScript interpreter to deal with. If applicable, try directly setting/getting the private variable itself rather implementing methods for getters, setters.
Tuesday, January 8, 2008
Monday, January 7, 2008
ASP.NET AJAX Best Practices: Reduce scopes
It's not pretty common. But, if you ever encounter such code, be sure it's a very bad practice. Introducing more scopes is a performance issue for JavaScript interpreter. It adds a new scope in the ladder. See the following sample scope:
function pageLoad()
{
scope1();
function scope1()
{
alert('scope1');
scope2();
function scope2()
{
alert('scope2');
}
}
}
Introducing more scopes enforces the interpreter to go through new more sections in the scope chain that it maintains for code execution. So, unnecessary scopes reduce performance and it's a bad design too.
Sunday, January 6, 2008
ASP.NET AJAX Best Practices: Avoid using your own method while there is one
Avoid implementing your own getElementById method that will cause script to DOM marshalling overhead. Each time you traverse the DOM and look for certain HTML element requires the JavaScript interpreter to marshalling script to DOM. It's always better to use getElementById of document object. So, before you write a function, make sure similar functionality can be achieved from some other built-in functions.
Friday, January 4, 2008
ASP.NET AJAX Best Practices: Careful with DOM element concatenation
It's a very common bad practice. We often iterate through array, build HTML contents and keep on concatenating into certain DOM element. Every time you execute the block of code under the loop, you create the HTML markups, discover a div, access the innerHTML of a div, and for += operator you again discover the same div, access its innerHTML and concatenate it before assigning.
function pageLoad()
{
var links = ["microsoft.com", "tanzimsaqib.com", "asp.net"];
$get('divContent').innerHTML = 'The following are my favorite sites:'
for(var i=0; i<links.length; ++i)
$get('divContent').innerHTML += '<a href="http://www.' + links[i] + '">http://www.' + links[i] + '</a><br />';
}
However, as you know accessing DOM element is one the costliest operation in JavaScript. So, it's wise to concatenate all HTML contents in a string and finally assign to the DOM element. That saves a lot of hard work for the browser.
function pageLoad()
{
var links = ["microsoft.com", "tanzimsaqib.com", "asp.net"];
var content = 'The following are my favorite sites:'
for(var i=0; i<links.length; ++i)
content += '<a href="http://www.' + links[i] + '">http://www.' + links[i] + '</a><br />';
$get('divContent').innerHTML = content;
}
Thursday, January 3, 2008
ASP.NET AJAX Best Practices: Use more "var"
Less use of "var" can result into wrong calculation as well as mistake in logic control. And also JavaScript interpreter finds it hard to determine the scope of the variable if var is not used. Consider the following simple JavaScript code:
function pageLoad()
{
i = 10;
loop();
alert(i); // here, i = 100
}
function loop()
{
for(i=0; i<100; ++i)
{
// Some actions
}
}
Here you see, the loop uses the variable i used before in pageLoad. So, it brings a wrong result. Unlike .NET code, in JavaScript variables can go along with the method calls. So, better not confuse the interpreter by using more "var" in your code:
function pageLoad()
{
var i = 10;
loop();
alert(i); // here, i = 10
}
function loop()
{
for(var i=0; i<100; ++i)
{
// Some actions
}
}
Make HTML controls discoverable in Volta Control
When a Volta control is rendered, the ID attribute of the generated HTML is changed to something like _vcId_1_DivName which is inconvenient to find from code. But the ID attribute stays the same in case of Volta Page, so it is discoverable by ID like this:
Div divContent = Document.GetById<Div>("divContent");
However, if you add HTML controls to the control like the following, the ID is not changed during the rendering:
public VoltaControl1() : base("VoltaControl1.html")
{
InitializeComponent();
Button btnClick = new Button();
btnClick.InnerText = "Click!";
btnClick.Id = "btnClick";
this.Add(btnClick);
}
If you don't prefer this way and seriously want to write your own HTML in the control's html page, you might find the following snippet useful. But, remember in this case you will use name attribute of the html element instead of ID.
// Usage: var element = GetElementByName(Document.GetElementsByTagName("div"), "divWidget");
private HtmlElement GetElementByName(HtmlElementCollection elements, string name)
{
foreach (var element in elements)
{
DomAttribute nameAttribute = element.Attributes.GetNamedItem("name");
if (nameAttribute != null)
if (nameAttribute.Value == name)
return element;
}
return null;
}
Wednesday, January 2, 2008
Making cross domain AJAX call using Volta
Making a cross domain AJAX call in Volta is piece of cake. Volta compiler generates necessary client codes to make it work. Here is a snippet that can make an AJAX call to some Url and fetch data:
public void DownloadPhotos()
{
IHttpRequest request = HttpRequestFactory.Create();
request.AsyncSend("POST", URL, string.Empty,
delegate(string response)
{
OnPhotosLoaded(new PhotosLoadedEventArgs(response));
});
}
Both IHttpRequest and HttpRequestFactory classes can be found in the Microsoft.LiveLabs.Volta.MultiTier namespace. AsyncSend method performs the asynchronous call and calls back the delegate defined where OnPhotosLoaded event is fired to notify the subscriber of this event that the data has just arrived.