January 12, 2008 by habesh
Today I have seen an odd disabled button behavior in IE browser
I know if a button is disabled it doesn’t respond to any mouse events, but check this out
<html>
<body>
<input type="button" value="button type" disabled="disabled"
onmouseover="alert('respond to mouse event');" />
<input type="submit" value="submit type" disabled="disabled"
onmouseover="alert('respond to mouse event');"/>
</body>
</html>
Both the above elements are disabled and are not clickable and hence you won’t see the alert message while hovering over these buttons(the onmouseover events won’t fire) but the second html button text(’submit type’) happen to be selectable and if you hold down the mouse and move it till the mouse is out of the button area and release the button then you will see the alert message. Very odd!!!!!
This didn’t happen for input type button. Both form elements should have behaved the same way as specified in the documentation http://msdn2.microsoft.com/en-us/library/ms533732(VS.85).aspx
Tags: HTML, Javascript
Posted in HTML, Javascript | Leave a Comment »
December 1, 2007 by habesh
A Cookie is a small amount of named data stored by the web browser and associated with a particular web page.
Things to watch for while implementing cookies.
- A total of 20 cookies per site or domain with a total max size of 4KB.
- A user could disable cookies in their browsers(although session cookies may still be enabled). You may want to first check that they are enabled before trying to store persistent cookies.
- Cookies default visibility is for all pages in the same directory as the page that created it and sub-directories of that directory. This could be changed by setting the cookie ‘path’ attribute.
- Cookies are accessible only to pages on the same domain.
- Cookies are stored on the client machine and are not shared by different browsers. eg. Cookies stored by IE is not visible to FF and vice verse.
Cookies are sent to the server on every HTTP request and could be manipulated both on server side using server side scripts and on client side using javascript. Below is a javascript methods to get, set and remove cookies
function set(name,value,day){
if(!name){
return;
}
var cookie;
var date = new Date();
cookie = name + “=” + encodeURIComponent(value);
if(typeof day === ‘number’){
date.setTime(date.getTime()+(day*24*60*60*1000));
cookie += “; expires=”+date.toGMTString();
//cookie += “; max-age=”+day*24*60*60;
}
cookie += “; path=/”;
document.cookie = cookie;
}
To set session cookie – set(“cookieName”, “cookieValue”);
To set persistent cookie(for 30 days) : set(“cookieName”, “cookieValue”, 30);
function get(name){
if(!name || document.cookie.indexOf(name) === -1){
return;
}
var curCookie;
var list = document.cookie.split(“; “);
for(var c=0;c<list.length;c++){
curCookie = list[c];
if(curCookie.indexOf(name + “=”) != -1){
return decodeURIComponent(curCookie.subString(name.length+1, curCookie.length));
}
}
}
To get a cookie – get(“cookieName”);
function remove(name){
set(name,”",-1);
}
To remove a cookie – remove(“cookieName”);
You may have noticed a commented line in the above ’set’ function that sets the max-age attribute(a way to specify the life time of a cookie according to HTTP 1.1 spec) of a cookie, though that may not work in IE so we should use a depreciated ‘expires’ attribute.
Posted in Cookie, Javascript | Leave a Comment »
November 19, 2007 by habesh
In one of my computer science course, our professor start the class by asking a question: ‘what will you take with you if you get deserted in an FP world?’ some of us mentioned some popular FP languages, book,…he then took out a lambda symbol off his pocket….this is what you should take with you…it is the basis of functional programming language.
In lambda calculus, every expression is a function and its arguments and return values are also a function. And Each function only take a single argument(curried functions).
To multiply two numbers [ f(x,y)=x*y;] in its curried form :
f x y . x * y;
var double = f 2; //parameter ‘2′ is partially applied to the multiplication function.
double 5; //apply the second parameter to the partial function, this will give us 10.
functional programming language focuses on the ‘what’ instead of the ‘how’ of a problem.
lets write a function to get all prime factors of a number n.
first lets create helper functions to create list of integers b/n lo to hi and a filter function to filter a given list based on a given criteria.
//create a list of integers from lo to hi(in x::xs, x is the first element and xs is the rest of the list)
fun fromTo lo hi =
if lo > hi
then []
else lo :: fromTo (lo+1) hi;
//return a list where the elements pass test f
fun filter f null = []
| filter f x::xs =
if f x
then x :: filter f xs
else filter f xs;
//return true if n is is divisible by i
fun divides n i = (n mod i == 0);
fun factors n = filter (divides n) (fromTo 2 (n – 1));
How expressive is that?
Posted in functionl programming | Leave a Comment »
November 16, 2007 by habesh
One of the best practices in javascript programming is avoiding global variables(they are evil), if you happen to have multiple javascript files from multiple authors they will lead to a name collision and hence unexpected result. Fortunately, there are great tools that would help us detect them.
It can be avoided by wrapping variables and functions in to an object. Anonymous functions are elegant ways of wrapping global variables and functions , and executing them immediately.
(function(){
var isIE = !!document.all;
var div = document.getElementById(“xyz”);
div.style.height = ….
})();
It will be very helpful to execute a block of initialization scripts, augmenting an existing object or setting it properties when a .js file is loaded.
Posted in Javascript | Leave a Comment »
November 16, 2007 by habesh
Functions that can accept functions as parameters and return functions are called Higher order functions. Javascript functions are one of these HOFs.
javascript functions return functions, that allows us to create a nested function within another function(closure)
take an example of advanced event registration of the following
Eventing.addEventListner(div,click,click_callback);
function Eventing(){
“addEventListener” : function(){
if(window.addEventListener){
return function(dom, eventName, callBack){
dom.addEventListener(eventName, callBack, false);
};
}
else if (window.attachEvent){
return function(dom, eventName, callBack){
dom.attachEvent(“on” + eventName, callBack);
};
}
}()
}
In the above example the anomymous function return an event handler function and the event handler function takes a callback function as a parameter. HOF are one of the powerful feature of javascript.
Posted in Javascript, functionl programming | Leave a Comment »
November 7, 2007 by habesh
It is 12:05 Am and i was reading this blog about marketing ‘the self’, which inspired to me to create an account and start blagging.
It is been a while since i have this thought of improving my communication still, express myself and sharing various ideas with people of similar interest. From now on you will see me blogging about various software development related areas like TDD, Scrum, Javascript, Ajax, C#,functional programming(it is cool! I’ll blog on it soon ).
Posted in Uncategorized | Leave a Comment »