Posts

Using javascript array function splice to modify array contents

Image
Javascript array object has lots of pre-defined functions that makes life easy. Among them, splice is the method lot of developers are not aware of. Using splice, you can remove an element in array, replace array contents without copying, iterating through entire elements. Prototype uses existence of splice method to identify whether an object is an array or not (Object.isArray). so, no need to worry about browser compatibilities. I used to iterate through entire array object to remove an element like the following v ar arrObj = ['1', '4', '3', '2']; var anotherArray = []; for (var i = 0;i <=arrObj.length; i++) { if(arrObj[i] != '3') { anotherArray.push(arrObj[i]); } } We need to iterate through entire array to remove an element if above method is followed. Using splice method, v ar arrObj = ['1', '4', '3', '2']; for(var i = 0; i <=arrObj.length; i++) { if(arrObj[i] == '3...

Retaining license details in javascript files while minifying using YUICompressor

Minification is one of the methods suggested by yahoo's yslow plugin . During minification process all comments in a javascript file are removed, long variables names are replaced with shorter ones, removing unwanted spaces, removing empty lines, making the minified file size as minimum as possible. If we are using a third party javascript file whose license should be intact while sending to the outside world. Using YUICompressor 's default setting it removes all the comments while include license info as well. If you want a comment block not to be removed by compressor while minifying, then that comment block should start with ! For example if the input is /*! this is my js should include license */ var myFunc = function() { /* this comment i dont want in minified version*/ ... } Then the output of compression will be like /*this is my js should include license */ var myFunc= function() {...} thus retaining the comment after minification as well.

Browser behaviour when 403 Response with Content-Disposition Attachment header

If client requests for a file download for which the logged in user does not have permissions, server should not set content-disposition:attachment header. If the header is set and if 403 (Forbidden) status code returned by the server, then client shows weird message as file not found.

Proper way to have an anchor tag with onclick event

Anchor tags are one of the most common HTML tags used in pages. If the user right clicks on the anchor tag he/she will be provided with an option of 'Open in new Window/Open in new Tab'. This will happen only if the anchor has href attribute. Sometimes it is required to do some other action (like making ajax request) on click of the anchor link and also provide the right click option for the same anchor. Natural practice by web developers. <a href="javascript:void(0);" onclick="alert('i am here');">Click Me! </a> Click Me without link for anchor! Correct way to add anchor tags without losing anchor properties. <a href="http://www.blogger.com/amareswar.blogspot.com" onclick="alert('i am here'); return false;">Click Me! </a> Click Me! In the above link, you can observe that the user can right click on go to the respective link and onclick will do the custom action provided by the developer.

My Son my life

Image
Life changed a lot after my son 'Harshith' came into my life. It is the feeling that you can nerver forget. I am enjoying the most happiest phase of my life. Missing him a lot as he is staying away from me with her mother. Hoping to be with him as soon as possible.

Prefer primitives over boxed primitives in java

This blog is one of the item in famous "Effective Java 2/e" by Joshua Bloch. In java, we got a flexibility of auto boxing and auto-unboxing of Java primitives like int,long,boolean etc.., int i=0; Integer j = i; In the above example i is autoboxed to j and the vice-versa is also possible. Consider the following example: public class TestClass { private static Integer i; public static void main(String[] args) { if(i == 42) { System.out.println("Unbelievable"); } } } If the above program is executed, what do you expect? it prints "unbelievable" ? no it wiil not but it behaves as strange are printing "unbelievable". It throws NullPointerException !!. What might be the reason? well this is caused because of one of the side effects of famous auto-boxing. When ever a primitive and boxed primitive involves in an operation, then auto-unboxing of boxed primitive happens. In the above example, i will be auto unboxed and it is a null refer...

Be careful before setting innerHTML

When working with javascript, the most common thing a developer will do for adding content by setting innerHTML of container element. ele.innerHTML ='<div><span>This is working fine</span>'; if(user == 'amar') { ele.innerHTML += '<span>Hello amar welcome to your blog!</span>'; } ele.innerHTML+='</div>'; Above code seems to be ok but there is a big mistake. Element's innerHTML is set and the div is not closed before evaluating the condition. Browser closes the div at the point and the span that is added in the condition will be sibling of div instead of child !!. This problem can be solved by using a variable, adding the content to that variable and finally setting innerHTML to the element.