Tuesday, April 19, 2011

How cookie works

A lot of developers know how to get / set cookie to persist their application, however, not necessarily all of them know how cookie actually works.  Here is a short tutorial on how cookie works.

When your browser send a request to a website "A", your client (browser, for example) will contact server "A" for contents.  Your browser will look on your machine whether a cookie that has been set by "A".  If it finds the "A"'s cookie, your browser will send all those related name-value pairs in the file to "A" as HTTP headers. If it didn't find the file, it won't send any cookie data ( well, that's obvious ).  After that, the "A" website can use the cookie that the browser send, and "A" can set additional cookies for future use.  Also, the cookie is a text string that is include in the request and response, that's how "A" can interact with your browser.


The following is a more visualize version of how cookie is set and get:

1) when you request a server by url a http requst will be sent ( assume this is the first time access )
============================
GET /index.html HTTP/1.1
Host: www.abc.com
============================

2) when server response, it will have something like that
============================
HTTP/1.1 200 OK
Content-type: text/html
Set-Cookie: name=iroy2000
============================

3) now the browser request the same page again
============================
GET /index.html HTTP/1.1
Host: www.abc.com
Cookie: name=iroy2000
Accept: */*
============================

Extra Note about Cookie
What's the difference between "session" and "cookie" ?
There are two kinds of cookies: session cookies and persistent cookies. The session cookies are stored in memory on the server, whereas persistent cookies are stored in a cookie file on the client.

Some developers, when they first start, may be confused about "session" and "cookie".  In short, "cookie" is saved in the client's browser and can last for a long time (it first saved in browser memory - "temp cookie", but if you set a long time-to-live value, the cookie will be saved into a file - "permanent cookie" ) , while "session" is saved in the server and only valid for a browser session.

What is a persistent (permanent) cookie?
A persistent cookie is a cookie which is stored in a cookie file permanently on the browser’s computer. By default, cookies are created as temporary cookies which stored only in the browser’s memory. When the browser is closed, temporary cookies will be erased. You should decide when to use temporary cookies and when to use persistent cookies based on their differences:
  • Temporary cookies can not be used for tracking long-term information.
  • Persistent cookies can be used for tracking long-term information.
  • Temporary cookies are safer because no programs other than the browser can access them.
  • Persistent cookies are less secure because users can open cookie files see the cookie values.

And depends on your architecture and your use case,  one method may be more preferable than the other one. Let me list some points that you should consider:

  1. How many servers you run your website?  Remember that "session" is saved in the server (yes, a server, not multiple server), so it means if you have more than one server in your cluster, you need to maintain the "session" for a particular user.  Usually people sync the "session" using the database, and I believe apache's mod_proxy provide sticky session that maintain sessions in multiple server settings.  However, cookie is saved in browser, so it won't care how many servers at your back.
  2. How sensitive is your data?  Since "cookie" is save in your local machine, it means your user could potentially modify those data.  For example, firefox users can modify their cookie values.  If you do not want your user to modify the information, and if the data is sensitive, it is recommended that you should not use cookie to hold the data, but instead, try to hold sensitive data in database and use cookie to hold the ID for future retrieval. 
  3. How long you need your data? Well, as mentioned above, session is for a browser session, cookie is for longer term.
  4. How large is your data? Cookie since it is saved in the browser, it has a size limitation, if you are going to store big amount of data, you should consider session or database, because these both saved in a server and has more generous limitation than session. 

Thursday, April 14, 2011

jQuery - check element if it's existed

This is the correct way of doing this

if($('.myClass').length > 0) {
    // blah ...
}

Some people ask, why the following doesn't work??

if($('.myClass')) {
    // blah ...
}

Well, it's because jQuery selector always returns you an Object. 

===

Of course, if you are using plain javascript, then it is correct.  For example,

var home = document.getElementById("home");

if(home) {
  // blah ...
}

Wednesday, April 13, 2011

Dangerous about Outreach as an end goal model


!! The following are just my personal opinions / observations. !! It is not pointing  to any particular groups or organizations, it is just about a potential of making things better for the future.

Definition
Outreach as an end goal model – words by words – means you have fulfilled the goal of bringing a good number of people into the church.  And you think it is a mission completed.

However, outreach by NO MEANS should be an end goal or just one of a particular isolated events of the year.  We are talking about “life” business here, not just bringing a good number of people to the church and that’s it.  Without a series of following ups for those new “outreachees”, we are no different from leaving them on isolated island and let them to play the game of “natural selection” – so at the end, only the fittest will stay.   Let me rephrase it, it means you bring people into church and isolating them??!!

And this actually makes outreach  become a poison for new believers and non-believers.  How?? One’s early experience / observations in the church (or a group)  could determine his perception of “what a church is” for his life time.   So what does it mean??  Perhaps it means we’re blocking God’s hand to touch those people’s heart…  Have you ever heard some people they saw bad things in the church and never went to church again???  Do you think it will be more difficult to outreach to those people who has been to church but had a bad experience before??

So when you think you have completed a “mission”, have you ever thought whose mission you are completing for??

You may not be the one who do both outreach and follow up at the same time, but are those 2 things should be tightly related to each other??

Let me make it more visualize – the dangerous of “Outreach as an end goal model”.  Let us make a “田”, if you know chinese – “田” means a farming field, and inside a “田” is soil.




Assumption for this model:
1) Outreach brings new life to the field.
2) Following up bring those new life to be future workers in the field who will bring other new life.


Detail – yet not very detail
Soil A is the best soil.  With both outreach and following up in shape, there is a eco-system of 1) Outreach – bring new life to the field, 2) follow up – make the new life become the future “workers” in the fields to outreach other new life.  And through this eco-system, there are several side benefits that comes with it.  1) Encouragement; 2) Bringing meaning to your serving; 3) Grow in size … etc.

Soil B is bad testimonial soil. People only do the outreach but neglect the importance of following up.   It means the plants could be die down for whatever reasons easily and they don’t have support.  What does it mean?? With no following up, unless they ask for help, nobody will know or  care what’s going on in their life.  With no following up, it means you bring people into church and  isolate them??!!

Soil C is in grown soil, which is bad too.  In this Soil C model, the church members become so comfortable with their current church life, and do not want to have a change. And this is why Bible said “Now is the harvest time, but where are the workers ?”.  The church will start to ingrown only, and it is the  beginning symptoms of a “no life church”.  No life means no new life, No life means there won’t be next generation leader to revive the church.  So Soil C is a ingrown shrinking land.

Soil D, please, that should never be happening in our community…  That is a social club.

Tuesday, April 12, 2011

Mobile (Hybrid) Apps Introduction

Been to several events that talk about mobile development recently.  Looks like the "web mobile apps" is the way to go in the future.  And the technology using here is quite simple and standardized - html5, css3 and javascript.  With the new HTML5, it provides lots of features like canvas, videos, geo-location, local storage ... etc, it makes developing a persistent web apps for mobile phone become highly doable.  And the techniques of developing web apps in mobile is not much difference than doing that in the browser, the only difference is the API (and device) you can access. Well, of course, doing web apps in the mobile has its limitation too, for example, performance is an issues.

However, there is a concept of Hybrid App, which converting your web apps into a native app for your mobile.  So in this way, you are still using html5, css and javascript (yep, you don't need to learn Objective-C ... etc) and yet your app will end up be compiled as native app and can access your phone features / devices.  And it solved the problems that a pure web mobile apps had. PhoneGap is one of the leaders in this field.

There are several links I found useful  for mobile web development, let me share it here

http://tinysrc.net/   - a free service convert your image into appropriate size on the fly
http://caniuse.com  - can you use certain feature in html5, css on a various platform

http://blitz.io/ - load testing for mobile
http://origwww.blaze.io/mobile/ - test your website performance in mobile devices
http://www.webpagetest.org/  - similar to above
http://punypng.com/ - a free web service to downsize your image without trading off quality

jQuery selectmenu - call back

The jQuery selectmenu  documentation is kind of confusing, it lacks of examples and documentations.  Well, it covers how to use this plugin in its basic way, but nothing more.

I came across this problem of firing callback when the menu is changed. The documentations says;
Callback events The selectmenu plugin provides callbacks for open, close, select, and change events, allowing you to execute scripting based on those events. The change event even triggers a change event on the original select element, allowing you to reliably track form change events.

Do you pass your call back in as one of your config parameter or what?? And finally it is how it works:

// You first need to initialize the select menu and bind your element
$('.menu-drop').selectmenu({ style: 'dropdown' });

// Now there are some new functions bind to ".menu-drop" that you can use

$('.menu-drop').change(function () {
        //either ajax call or whatever you want
});


Well, of course, you should be able to chain that (recommended) ~

$('.menu-drop').selectmenu({ style: 'dropdown' }).change(function () {
        //either ajax call or whatever you want
});


Monday, April 11, 2011

Javascript code only works in firefox when firebug open???

Symptoms: Your javascript code works only in FF when firebug is turned on.

Solution:  Look inside your code and see if you have console.log or something like that in there.  Because when firebug is turned off, the console is becoming undefined.

If you must have the console in your code, try to wrap your console in something like this:

if(window.console) {
    console.log();
}

IE throws Invalid Argument when calling window.open() ??

In my javascript code, I try to open a new window to hold a div ( a block of contents ) for printing.  And IE throws me an "Invalid Argument" error ( while other browsers are fine, which is usually the case ~ ).

I search online, there are 2 different methods to deal with this problem:

1)   var w = (window.open(urlstring, wname, wfeatures, false));

2) You cannot have 'space' in between "wname" items in IE.

I tried the first one, it doesn't work; and I tried the second 
one, and it works.

For the method one and its explanation, here is the link
http://stackoverflow.com/questions/710756/ie8-var-w-window-open-message-invalid-argument