Archive for the 'programming' Category

javascript void(0) for HTML link

May 27, 2012

To create a HTML link which does nothing when user clicks it,  use  like below


<A HREF="javascript:void(0)">Click here to do nothing</A>

When the user clicks the link, void(0) evaluates to undefined, which has no effect in JavaScript.

References -

https://developer.mozilla.org/en/JavaScript/Guide/Expressions_and_Operators#void

Here is a stackoverflow.com link, which answers why  void(0)  is better than using #  for this requirement

http://stackoverflow.com/questions/134845/href-for-javascript-links-or-javascriptvoid0

Referencing variables in setTimeout function – Javascript Closures

May 26, 2012

Requirement

- Reference For loop variable i  in setTimeout function

Wrong way


for(var i = 1 ; i <= 10; i++) {
console.log('i in for :: ' + i);
setTimeout(function() {
console.log(' i in timeout :: ' + i);
}, 2000);
}

Ouput


i in for :: 1
i in for :: 2
i in for :: 3
i in for :: 4
i in for :: 5
i in for :: 6
i in for :: 7
i in for :: 8
i in for :: 9
i in for :: 10
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11
i in timeout :: 11

Right way using Closures


for(var i = 1 ; i <= 10; i++) {
console.log('i in for :: ' + i);
setTimeout(function(j) {
return function() {
console.log(' j in timeout :: ' + j);
}
}(i), 2000);
}

Ouput


i in for :: 1
i in for :: 2
i in for :: 3
i in for :: 4
i in for :: 5
i in for :: 6
i in for :: 7
i in for :: 8
i in for :: 9
i in for :: 10
j in timeout :: 1
j in timeout :: 2
j in timeout :: 3
j in timeout :: 4
j in timeout :: 5
j in timeout :: 6
j in timeout :: 7
j in timeout :: 8
j in timeout :: 9
j in timeout :: 10

References  -
http://jibbering.com/faq/notes/closures/

https://developer.mozilla.org/en/JavaScript/Guide/Closures

javascript undefined, null, undefined property, 0, false, ”, ==, ===, typeof

March 22, 2012

== Equals

If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison.

=== Strict equal

Returns true if the operands are strictly equal  with no type conversion.

typeof

The typeof operator returns a string indicating the type of the unevaluated operand.

typeof undefined === ’undefined’

undefined

A variable that has not been assigned a value is of type undefined.

false

Examples of expressions that can be converted to false are those that evaluate to null, 0, the empty string (“”), or undefined.

null

null is an object. It’s type is null. Null and Undefined types are == (but not ===)

Undeclared variable

  1. /* var z*/
  2. z.x  // throws a ReferenceError

Check for undefined variable

  1. var x;
  2. if (x === undefined) {
  3.    // these statements execute
  4. }
  5. else {
  6.    // these statements do not execute
  7. }

check for undeclared variable

  1. // x has not been defined before
  2. if (typeof x === ’undefined’) { // evaluates to true without errors
  3.    // these statements execute
  4. }
  5. if(x === undefined){ // throws a ReferenceError
  6. }

check for object property

  1. var myobj = {‘a’:'a’};
  2. myobj.hasOwnProperty(‘a’) // true
  3. var myobj2 = Object.create(myobj);
  4. myobj2.hasOwnProperty(‘a’) //false
  5. ‘a’ in myobj2 // true

check for null

  1. var c = null
  2. if (c === null) { // true
  3. }

References

http://saladwithsteve.com/2008/02/javascript-undefined-vs-null.html

http://constc.blogspot.com/2008/07/undeclared-undefined-null-in-javascript.html

http://www.mapbender.org/JavaScript_pitfalls:_null,_false,_undefined,_NaN

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/undefined

https://developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operators

https://developer.mozilla.org/en/JavaScript/Reference/Operators/typeof

http://www.nczonline.net/blog/2010/07/27/determining-if-an-object-property-exists/

Apache Ant target execute based on condition

July 21, 2011

To execute an Ant target based on condition, use the “if” attribute of the target,


<target name="build-module-A" if="module-A-present"/>

if the module-A-present property is set (to any value, e.g. false), the target will be run.

http://ant.apache.org/manual/targets.html#targets


<target name="use-file" depends="-check-use-file" if="${file.exists}">
</target>

As of Ant 1.8.0, you may instead use property expansion; a value of true (or on or yes) will enable the item, while false (or off or no) will disable it. Other values are still assumed to be property names and so the item is enabled only if the named property is defined.

http://ant.apache.org/manual/properties.html#if+unless

Apache mod_rewrite NE|noescape

August 27, 2010

I faced an issue where the URL for example /saved/ was getting rewritten to %2Fsaved%2F, which caused the web application to give 404 error because it expects the URL without escaped/encoded characters.

I found this Stack Overflow thread related to this problem which gave the solution,

http://stackoverflow.com/questions/2443115/apache-mod-rewrite-tomcat-encoding-26-and

Add NE flag to prevent escaping –

http://httpd.apache.org/docs/2.2/rewrite/rewrite_flags.html#flag_ne

Sun rises in the east

August 4, 2010

Recently I read a slashdot article – http://it.slashdot.org/story/10/07/28/2121259/Oracles-Java-Company-Change-Breaks-Eclipse about Java 1.6.0_21 release crashing Eclipse IDE because the IDE has code which checks for “Sun Microsystems” in the JVM company field property. This made me think about whether we should write code to handle these rare scenarios or things we take for granted that they will not change. In my opinion if we know or can predict that something like this will happen then handle it in code, else deal with it when it comes.

Follow

Get every new post delivered to your Inbox.