Friday, April 9, 2010

Javascript: Always define variable with 'var'

So today, I found one more unseen behavior of Javascript (at least for me).

I wrote a very simple function in Javascript and forgot to add 'var' in front of variable definition. This function worked first time but for second time I got JS error saying the function does not exist. I was like what the hell, where did my function go? It just worked as expected for first time and suddenly browser is not able to find it.

It took me 20 minutes to find out the reason of my biggest mistake of tonight. Such a waste of precious 20 minutes at 3:00 AM :(

Lesson learned, always define variables in Javascript with 'var' keyword or else stay up till late night ;)

Tuesday, April 6, 2010

Install Memcached on Windows 7

To install Memcached on Windows 7 you need administrative rights. Grab your copy of Memcached for Windows (I got it from here, so I will explain this one). Now follow the steps below:
  1. Download and unzip Memcached binaries.
  2. Press the Win key, type cmd, press Ctrl+Shift+Enter.
  3. Select yes from elevation prompt.
  4. A command window will open with administrative rights.
  5. Go to Memcached folder (mine is c:\memcached\)
  6. Type and run [ c:\>memcached -d install ], this will install Memcached as service.
  7. Now start the service with [ c:\>memcached -d start ]
  8. FIN
Now Memcached is installed and running.

Enjoy!!

Saturday, November 21, 2009

Less know JavaScript operator 'Delete'

One of the lesser known operators in JavaScript is the 'delete' operator. I came to know about while reading John Resig's blog.

What does it do?
The delete operator deletes an object, an object's property, or an element at a specified index in an array.

More about it at Mozilla Reference Docs.

Small Example:
var array = {
"int": 42,
"float": 12.5,
"string": "hello world",
"foo" : "bar"
}
for(var arr in array) {
alert(arr + " : " + array[arr]);
}
delete array['string']; //Removes the 'string' key of the array

for(var arr in array) {
alert(arr + " : " + array[arr]); //The 'string' key & its value will be removed
}

Wednesday, September 2, 2009

IE Conditional Tag with XSLT

Another hack in the life of UI developer, dont know for how long we have to take this burden.

Well, you are here because you are using XSLT and have noticed that IE conditional tag or comment is not showing up in output of XSLT transformation. What could be the reason?

Reason is that XSLT transformation does not output comments given in XSL sheets to HTML stream. So how to use IE conditional tags when you really need them.

The answer is <xsl:comment> tag + CDATA (Unparsed Character Data) tag. Following is the example showing correct way of doing it:

before (not working):
<!--[if IE]> something special for Internet Explorer <![endif]-->

after (working perfectly):
<xsl:comment><![CDATA[[if IE]> something speical for Internet Explorer <![endif]]]></xsl:comment>

In the example above we are using <xsl:comment> tag to output <!-- --> to HTML stream and using CDATA type to outupt rest of the code inside the comment tags. So one little hack and desired output presented.

JavaScript Error: Expected Identifier in IE

If you are getting 'Expected Identifier' JavaScript error on IE this means you have some conflict with Mr. Gates :)

Okay to the point, this can happen due to two reasons (as per my knowledge, let me know if you know something else):

1. Some typing mistake in your code

var myVar = {
function1: function() { // some code },
variable1: 'Some value',
variable2: 'Another value',
};

Notice the extra comma after the last variable defined just before } braces, Firefox will ignore this but IE will not. Lets move to second reason.

2. You have used reserved keyword in your code mostly as variable name
Just yesterday, I got this error on IE due to some old written JS code. First I tried to find the code mistake but after looking at the code with a sharp eye I found that some reserved words were used. Following is the code I corrected

obj.class = 'up'; ///this works in Firefox
obj.className = 'up'; ///this is the correct implementation

var onloadstat = 0; //works in FF but in IE its a reserved keyword same as 'class'

At the end, if you get the 'Expected Identifier' JS error in IE first look for these mistakes. You will be able to fix the error quickly.

About my blog

This blog will be having contents for my own reference so that I don't have to go to Google again to search for the stuff.

I will also be happy if someone is taking benefits from my knowledge. If you like the blog, don't forget to leave a comment :)