jQuery good practice-cache jQuery selector

Hi Folk,

Nowadays, lots of us use jQuery for front-end development and found the bad practice.

i.e. 

<div> Programming Language </div>

<ul id="language">

  <li>Basic</li>

  <li>C++</li>

  <li>Cobol</li>

  <li>Java</li>

  <li>Pearl</li>

  <li>Python</li>

  <li>PHP</li>

  <li>Ruby</li>

  <li>Scala</li>

</ul>

//Bad practice

//Give yellow background to Basic 

 1)  $("#language").children(": first").css({"background-color":"yellow"});

//Change Basic to Q-Basic

 2)  $("#language").children(":first").html("Q-Basic");

 //Adding new language to list

 3)  $("#language").append("<li>Cold Fusion</li>");

//Good practice

//Cache the language element

   var lngObj = $("#language");

   //Cache the first children so that it can be reused later on...

   var firstChild = lngObj.children(":first"); //There are plenty of ways to get First children..!!

   1) PLUS 2)

   //This is called chaining(calling more than one methods on single object)

   firstChild.css({"background-color":"yellow"}).html("Q-Basic");

 3) lngObj.append("<li>Cold Fusion</li>");

//Why it is important

Everytime whenever Jquery selector is created, it needs to traverse/Scan the DOM to find out reference to that element and it costs processing and time so degrade the speed.

Twitter had an issue in past for non responsive UI because of above.

This matters when there are thousand of elements in DOM..!!!!

 

 

Posted in Javascript, jQuery | Leave a comment

how to convert integer to string in javascript

Hi folk,

I needed to convert integer to string in javascript.

var product_quantity = 3 ;

so what if i want to convert it into string. I needed to convert because jQuery’s html() function take string as a parameter.

product_quantity = product_quantity + "" ;

That means, when concating with string , javascript engine makes it string and append the operands instead of performing addition operation.

product_quantity = new String(product_quantity);

I’m not sure but above should also work…

And for reverse conversion, you might be knowing about parseInt() !!

 

 

 

Posted in Javascript, jQuery, object | Leave a comment

How to install ubuntu on window xp using Virtual box ?

Step 1 : Get the virutual box from the VirtualBox website’s download page

Step 2 : Get the ubuntu iso file from  http://releases.ubuntu.com/

Here it will list out up to date avialable versions of ubuntu. Go for any. I picked the latest one.    (Ubuntu 11.04 (Natty Narwhal))

ubuntu release page gives different options based on machine configuration(32-64 bit)

Step 3: Install virtual box that you have downloaded and configure it. During installtion process, it will ask you to allocate howmuch of memory(RAM)  and disk space you want to use in virtual box

Step 4: Once virual box installtion gets complete, then we can load the iso file that we downloaded in step 2.

Click on Settings and Storage. Then, under CD/DVD Device, next to Empty, you’ll see a little folder icon. Click that, and you can select the Ubuntu .iso you downloaded earlier.

Step 5 : Once you’re done with it, click OK.

Step 6: Now virtual box will start installing ubuntu.(Follow the same steps as regular installation procedure of ubuntu)

Step 7: At last, when ubuntu is installed, you need to  change the CD/DVD Device entry(in step 4) to be Empty again

For more information, check  psychocats .

Congrates, You’re done..!!!

Posted in ubuntu | 1 Comment

quick fix to load google map inside hidden div

If your web page need to show google map base on certain event excepting onload then you may think to load google map inside a hidden div or any other html elements. And show the map on certain event.i.e. clicking button or link..

Doing so what happens is you will get a weird small visible map view area with large blank gray background.

Interesting thing is that if you resize the browser window then it would be fixed!!

So what is exactly needed to be done to fix this problem is, take one global variable along with your google map code. I’m using the google maps V3 API.

I’m not writing whole google map code.

var IsGoogleMapLoaded=false;

Bind an extra event to ensure that map is loaded.

google.maps.event.addListener(your_goolgle_mapObj, 'idle', function () {

				if (!IsGoogleMapLoaded) {

            				IsGoogleMapLoaded = true;

				}

			});

And bind the user event as below to load the map and show it.

if(IsGoogleMapLoaded == false)
	$("#mapCanvas").fadeIn(initialize);
else
	$("#mapCanvas").fadeIn();

Here initialize()  is function that instantiate google map object with necessary setting..

And its done…

 

 

 

Posted in google maps, Javascript, jQuery | 2 Comments

aborting ajax request using jQuery’s global ajax event handlers

Currently, I needed to abort the jQuery ajax request based upon certain conditions. My app uses plenty of $.post(). $.post() is actually an abstract of $.ajax(). And what $.post() lacks is we cant specify other settings like attach error handler !!

It is cumbersome to translate those $.post() into $.ajax(). If I do that then I can specify “beforeSend”  an ajax event inside $.ajax(). What “beforeSend” takes as arguments is jQueryXmlHttpRequest(jqXHR) object as first argument and setting as second argument. The setting here is the same as setting provided to $.ajax() method.

i.e.

$.ajax(  {

beforeSend:function(jqXHR,setting)

{

//check the url or other conditional checks

if(setting.url == "my restricted url")

//Abort request

jqXHR.abort();

// OR

return false;

}

});

We can either specify abort() or return false to abort above ajax request.

It takes lots of work to do for each ajax handler and that is not feasible.

So jQuery’s global ajax event handlers come for rescue. They acts as a hook for existing ajax handler.No matters whether it is $.ajax() or $.post() or $.get() or something else.

I used .ajaxSend() to act as a wrapper for all existing ajax call across the app. It attaches a function to be called before sending ajax request.

$(document).ajaxSend(function(e,jqXHR,setting)

{

//check the url or other conditional checks

if(setting.url == "my restricted url")

jqXHR.abort();//abort request

});

We can also achieve same result using jQuery.ajaxPrefilter() but which requires jquery 1.5.

That’s all..

Posted in Javascript, jQuery | Leave a comment

my first jQuery app

Well, here I’m gonna tell you to make the first jQuery app.

Step 1 : First thing you need is to download the jQuery from http://jquery.com/.

If you get stuck there to select the appropriate version(jQuery comes in 2 different flavors. One is minified,gzipped format and another is uncompressed) of jQuery then here is the simple tips.

You will notices the different in size in both type of jQuery files.

The gzipped,minified version is the light weight and used for production purpose. Minified itself is a self explanatory .  If you dare to open that file then you will find the continuous long line of javascript with 0% of readability as it uses the single letter of alphabets for function arguments and variables.

For the development purpose and If you are  curious to know how the stuff are working under the hood then use the uncompressed version of jQuery.

We can even load the jQuery from the CDN (Content Distribution Network). i.e. google,microsoft

Step 2:

<script src=”path to jquery file from local machine or CDN”></script>

<script>

$(document).ready(

function(){

//Your code goes here…..

console.log(“This is my first jQuery app…!!!! jQuery Rocks…”);

}

);

<script>

And you done…!!!

Posted in jQuery | Leave a comment

Thing you should know before learning jQuery

As web is growing rapidly, speed became the vital part of today’s web.

So “java-script”  is on rescue.  Javascript is faster and executes on client side(i.e. web browsers).

the advent of web 2.0, Javascript is getting more popular nowadays. And its community is growing a light speed.

What basic things you should know before getting start A,B,C,D of jQuery  are

1) Javascripts

javascript language specifications i.e. datatypes, variables,functions,objects,closures.. I guess thats enough!!

2) CSS (Cascade Style sheets)

3) HTML (Hyper text markup language)

4) DOM (Document Object Model)

DOM is a structural representation of a document.

For instant, you have created simple html page with some tables inside it. So when it is rendered by the browser,  it now becomes the part of DOM. All the html elements including table,tr,td are a part of DOM. Javascript works  with DOM to add,update, delete a specific part of web page.

 

 

Posted in Javascript, jQuery | Leave a comment

Hello folks!

Welcome to  Objects Farm….!!!

I will walk and talk about almost all kind of objects….. Objects not in the sense that different computer can understand but the real world objects………….

Feel free to discuss about each aspect of the object…

 

 

Posted in object | 1 Comment