Posts

Knowing whether a javascript code is being executed inside an iframe

Sometimes it is required to know whether the code is being executed inside an iframe or not. Following code works for all browsers. var inIframe=false; try { inIframe = window.top.document == document; }catch(ex) { inIframe = true; } The reason for keeping try catch in the above logic is when ever window.top.document is accessed and if that document belongs to different domain, then IE throws Access Denied error.

Accessing state property of xmlhttprequest before readyState 4 in IE

Like IllegalStateException in java, IE also throws Undefined Error when you try to access xmlhttprequest's status property before reaching steady state 4. For example in the following code if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { var status = xmlhttp.status; var isSuccess = status >= 200 && status if (xmlhttp.readyState==4 && isSuccess) { alert(xmlhttp.responseText); } } alert("in ajax"); ...

JQuery filesize plugin

Converting raw bytes size into human readable string is a very frequently used task. I developed a plugin that converts raw bytes into KB or MB and its usage is very simple. Suppose if you want to show 1000 bytes in human readable form inside a span assign the value as size attribute <span class="jqfilesize" size="1000"></span> and use jQuery(".jqfilesize").filesize(); output will be <span class="jqfilesize" size="1000">1 KB</span> Download the plugin here

Ellipsis class that works in almost all browsers

Applying ellipsis class will put three dots at the end of single line of the content. Remember that it works only for block elements. .ellipsis { white-space: nowrap; overflow:hidden; text-overflow:ellipsis; -o-text-overflow:ellipsis; -ms-text-overflow:ellipsis; -moz-binding:url("ellipsis-xbl.xml#ellipsis") } and ellipsos-xbl.xml is required for firefox

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.