Category Archives: javascript

Building Large Scale Javascript Application

Digital World 2014 was a 4 day (4th – 7th June, 2014) ICT event at Bangladesh. It was organized by ICT Division of Telecom and IT Ministry of the country. There was a full series of technical sessions with various interesting topics throughout the event.

One of them was Building Large Scale Javascript Application. It was presented by me together with Emran Hasan, CTO of eMicrograph. Here is the slide that I prepared in very short time.


Continue reading

Backbone.Marionette + RequireJS + Bootstrap boilerplate

Few days ago, I was searching around for a boilerplate setup for Backbone.Marionette with RequireJS. I was looking for something that is ready for use instantly without any prerequisite or additional dependency. I found few good solutions. But unfortunately none of them seems to be as simple and straightforward as I was expecting.

marionette is ready

So, I had to write my own boilerplate.
Continue reading

jQuery and jQuery Mobile on JSCon 2011, Dhaka

PHPXperts group  is one of the most active PHP community in Asia. They have arranged the first JavaScript Conference of Bangladesh on September 17, 2011 at AIUB Auditorium. It was a cool event with 250+ participants.

Our (me and Mohammad Zakir Hossain Raju) session was about jQuery and jQuery Mobile. Here is the slide –

BTW, SlideShare has featured this slide on homepage.

Continue reading

jQuery – Presentation at phpXperts Seminar, 09

At the phpXperts seminar 2009, I gave a talk on  “Blessings of modern javascript framework – jQuery” and it seemed well-received. I enjoyed giving the presentation very much!
Special thanks for all the organizers, group members and participants.

Here is the slide available for you.

Hope you’ll Enjoy the slides! Suggestions are welcome.

Extjs quick start guide for jQuery Developers

I’ve seen many people (including myself) trying out the power of jQuery for once and then getting stuck with it. Why not? It’s one of the coolest and smartest JavaScript library out there. However, I have broken out from the circle and found that Extjs is another great mentor in the field of JavaScript libraries. Especially, I think it’s UI components are unbeatable (Dojo can be a nearest candidate).

If you are habituated to think JavaScript coding in the syntax of jQuery, you can start working with ExtJS right now (with a few twist)!  What you need is some little tuning in concept and syntax. Today I will be trying to explain how to do this in 6 important points :

  1. Document is ready – How to get the our document ready and where to start.
  2. Selecting elements – How to select elements in Ext.
  3. Dom scripting – Changing on and in the element.
  4. Ext events – Assigning and firing events.
  5. Ext Components – The powerful alternate of jQuery UI.
  6. Ajax – Making Ajax request in Ext.

Ok, let’s dive in to deep of each of these points.

Continue reading

Front-end optimization with PHP Speedy – increase Yslow grade in 10 minutes!

Before I move into PHP Speedy, a small background won’t hurt much! Until a few days back, whenever I hear the word optimization, my brain quickly shows a few keywords in my mental screen: “query optimization, page caching, opcode, etc”. All these terms and the associated work relates to the optimization of a web application in the back-end. This has been serving my need pretty good, until I became aware about the excellent 13-point front-end optimization techniques from Yahoo (thanks Emran bhai).

At first I was a bit skeptical, but as soon as I started seeing the points – I knew its importance. Without much delay, I gave it a short in my latest project at work. And the tool that made it a less than 10 minute work is: PHP Speedy. I am literally speechless with it’s performance and way of working. Just in 10 minuets, It upgraded the Yslow grade of our project from 43 to 88.  Just like a magic touch!

php_speedy_logo_medium

Now, let’s see what Yslow Grade is (for those who are not yet using it) and how php-speedy helps to upgrade it. Hope this enlightens someone like me :P

Continue reading

Interactive character limit for textarea using Jquery

For a textbox, character of a field can easily be limited with maxlength attribute. But maxlength doesn’t work for  a textarea. So, It needs an alternate way. For one of my project, I wrote a function for this purpose where I used jQuery. Here I am explaining how I did it.

Let’s look an example of this interactive solution here

OK. Now we will make this in 2 easy steps.

1. Import your jQuery file and write the function "limitChars(textid, limit, infodiv)" in the head section of your page.

This function takes 3 parameters. They are:

  • textid : (string) The ID of your textarea.
  • limit : (num) The number of character you allow to write.
  • infodiv : (string) The ID of a div, in which limit information will be shown .

  Here is the function:

<span style="color: #606060"> 1:</span> &lt;script language=<span style="color: #006080">&quot;javascript&quot;</span> src=<span style="color: #006080">&quot;Jquery.js&quot;</span>&gt;&lt;/script&gt;
<span style="color: #606060"> 2:</span> &lt;script language=<span style="color: #006080">&quot;javascript&quot;</span>&gt;
<span style="color: #606060"> 3:</span> <span style="color: #0000ff">function</span> limitChars(textid, limit, infodiv)
<span style="color: #606060"> 4:</span> {
<span style="color: #606060"> 5:</span> <span style="color: #0000ff">var</span> text = $(<span style="color: #006080">'#'</span>+textid).val(); 
<span style="color: #606060"> 6:</span> <span style="color: #0000ff">var</span> textlength = text.length;
<span style="color: #606060"> 7:</span> <span style="color: #0000ff">if</span>(textlength &gt; limit)
<span style="color: #606060"> 8:</span> {
<span style="color: #606060"> 9:</span> $(<span style="color: #006080">'#'</span> + infodiv).html(<span style="color: #006080">'You cannot write more then '</span>+limit+<span style="color: #006080">' characters!'</span>);
<span style="color: #606060"> 10:</span> $(<span style="color: #006080">'#'</span>+textid).val(text.substr(0,limit));
<span style="color: #606060"> 11:</span> <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;
<span style="color: #606060"> 12:</span> }
<span style="color: #606060"> 13:</span> <span style="color: #0000ff">else</span>
<span style="color: #606060"> 14:</span> {
<span style="color: #606060"> 15:</span> $(<span style="color: #006080">'#'</span> + infodiv).html(<span style="color: #006080">'You have '</span>+ (limit - textlength) +<span style="color: #006080">' characters left.'</span>);
<span style="color: #606060"> 16:</span> <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;
<span style="color: #606060"> 17:</span> }
<span style="color: #606060"> 18:</span> }
<span style="color: #606060"> 19:</span> &lt;/script&gt;

Remember, the script is using jQuery. So be careful about the path of jquery at first line of this script.
 
2. Bind the function to the keyup event of your textarea. Do this in jQuery’s ready event of document. Just like this:
<span style="color: #606060"> 1:</span> $(<span style="color: #0000ff">function</span>(){
<span style="color: #606060"> 2:</span> $(<span style="color: #006080">'#comment'</span>).keyup(<span style="color: #0000ff">function</span>(){
<span style="color: #606060"> 3:</span> limitChars(<span style="color: #006080">'comment'</span>, 20, <span style="color: #006080">'charlimitinfo'</span>);
<span style="color: #606060"> 4:</span> })
<span style="color: #606060"> 5:</span> });

Here my textarea’s id is ‘comment’ , limit of characters is 20 and limit information will shown in a div whose id is ‘charlimitinfo’. That’s all, we have made an "Interactive character limiter" for our textarea using Jquery.
 
If you are not using jQuery for your site, it will not be wise to include it only for this script. You can get the same functionality from javascript without jquery. Look at this example. It looks like and works 100% as the previous though not using jquery.
Here you need just 2 changes. First, change the function "limitChars" as follows :
<span style="color: #606060"> 1:</span> <span style="color: #0000ff">function</span> limitChars(textarea, limit, infodiv)
<span style="color: #606060"> 2:</span> {
<span style="color: #606060"> 3:</span> <span style="color: #0000ff">var</span> text = textarea.value; 
<span style="color: #606060"> 4:</span> <span style="color: #0000ff">var</span> textlength = text.length;
<span style="color: #606060"> 5:</span> <span style="color: #0000ff">var</span> info = document.getElementById(infodiv);
<span style="color: #606060"> 6:</span>&#xA0; 
<span style="color: #606060"> 7:</span> <span style="color: #0000ff">if</span>(textlength &gt; limit)
<span style="color: #606060"> 8:</span> {
<span style="color: #606060"> 9:</span> info.innerHTML = <span style="color: #006080">'You cannot write more then '</span>+limit+<span style="color: #006080">' characters!'</span>;
<span style="color: #606060"> 10:</span> textarea.value = text.substr(0,limit);
<span style="color: #606060"> 11:</span> <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;
<span style="color: #606060"> 12:</span> }
<span style="color: #606060"> 13:</span> <span style="color: #0000ff">else</span>
<span style="color: #606060"> 14:</span> {
<span style="color: #606060"> 15:</span> info.innerHTML = <span style="color: #006080">'You have '</span>+ (limit - textlength) +<span style="color: #006080">' characters left.'</span>;
<span style="color: #606060"> 16:</span> <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;
<span style="color: #606060"> 17:</span> }
<span style="color: #606060"> 18:</span> }

 
And finally, call the function manually on keyup event of your textarea. Use "this" keyword for textarea instead of id when calling the function. example:
<span style="color: #606060"> 1:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">textarea</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;comment&quot;</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">=&quot;comment&quot;</span> <span style="color: #ff0000">onkeyup</span><span style="color: #0000ff">=&quot;limitChars(this, 20, 'charlimitinfo')&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #606060"> 2:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">textarea</span><span style="color: #0000ff">&gt;</span>

Finished. We made the solution again without dependency of any javascript framework.

Please let me know if you have an idea to make it better. 

jQuery controlled Dependent (or Cascading) Select List 2

Thank you all. Thanx for using, commenting and visiting my script jQuery controlled Dependent (or Cascading) Select List from my old bolg http://php4bd.wordpress.com and here.

I am getting a lots of request and questions from visitors about adding some features and some other problems. Sometimes I faced some problem when using this script myself. So, I have made some changes here. I hope, this change will help you to overcome some problems of previous version of this script. The main features added in this version are:

  1. It will keep in child list box only sub items of selected parent value when initializing.
  2. Added a 4th parameter to input a selected value for child list box.
  3. When "isSubselectOptional" is true, add a default value ‘none’ for ‘– Select –‘ option of child list box.
  4. Automatically focus the child list box when a value is selected from parent list box.
  5. More effective for multilevel association.

Click here to see a demo of multilevel association using this function. here is same demo for jquery 1.3.x users.

Here is the modified function (for < jquery 1.3):

<span style="color: #606060"> 1:</span> <span style="color: #0000ff">function</span> makeSublist(parent,child,isSubselectOptional,childVal)
<span style="color: #606060"> 2:</span> {
<span style="color: #606060"> 3:</span> $(<span style="color: #006080">&quot;body&quot;</span>).append(<span style="color: #006080">&quot;&lt;select style='display:none' id='&quot;</span>+parent+child+<span style="color: #006080">&quot;'&gt;&lt;/select&gt;&quot;</span>);
<span style="color: #606060"> 4:</span> $(<span style="color: #006080">'#'</span>+parent+child).html($(<span style="color: #006080">&quot;#&quot;</span>+child+<span style="color: #006080">&quot; option&quot;</span>));
<span style="color: #606060"> 5:</span> 
<span style="color: #606060"> 6:</span> <span style="color: #0000ff">var</span> parentValue = $(<span style="color: #006080">'#'</span>+parent).attr(<span style="color: #006080">'value'</span>);
<span style="color: #606060"> 7:</span> $(<span style="color: #006080">'#'</span>+child).html($(<span style="color: #006080">&quot;#&quot;</span>+parent+child+<span style="color: #006080">&quot; .sub_&quot;</span>+parentValue).clone());
<span style="color: #606060"> 8:</span> 
<span style="color: #606060"> 9:</span> childVal = (<span style="color: #0000ff">typeof</span> childVal == <span style="color: #006080">&quot;undefined&quot;</span>)? <span style="color: #006080">&quot;&quot;</span> : childVal ;
<span style="color: #606060"> 10:</span> $(<span style="color: #006080">&quot;#&quot;</span>+child+<span style="color: #006080">' option[@value=&quot;'</span>+ childVal +<span style="color: #006080">'&quot;]'</span>).attr(<span style="color: #006080">'selected'</span>,<span style="color: #006080">'selected'</span>);
<span style="color: #606060"> 11:</span> 
<span style="color: #606060"> 12:</span> $(<span style="color: #006080">'#'</span>+parent).change( 
<span style="color: #606060"> 13:</span> <span style="color: #0000ff">function</span>()
<span style="color: #606060"> 14:</span> {
<span style="color: #606060"> 15:</span> <span style="color: #0000ff">var</span> parentValue = $(<span style="color: #006080">'#'</span>+parent).attr(<span style="color: #006080">'value'</span>);
<span style="color: #606060"> 16:</span> $(<span style="color: #006080">'#'</span>+child).html($(<span style="color: #006080">&quot;#&quot;</span>+parent+child+<span style="color: #006080">&quot; .sub_&quot;</span>+parentValue).clone());
<span style="color: #606060"> 17:</span> <span style="color: #0000ff">if</span>(isSubselectOptional) 
<span style="color: #606060"> 18:</span> $(<span style="color: #006080">'#'</span>+child).prepend(<span style="color: #006080">&quot;&lt;option value='none'&gt; -- Select -- &lt;/option&gt;&quot;</span>);
<span style="color: #606060"> 19:</span> $(<span style="color: #006080">'#'</span>+child).trigger(<span style="color: #006080">&quot;change&quot;</span>); 
<span style="color: #606060"> 20:</span> $(<span style="color: #006080">'#'</span>+child).focus();
<span style="color: #606060"> 21:</span> }
<span style="color: #606060"> 22:</span> );
<span style="color: #606060"> 23:</span> }

Again, here is the function for jquery 1.3.x. Thanks a lot to TuxX for tuning up it to work with jquery 1.3.x.

[javascript]
function makeSublist(parent,child,isSubselectOptional,childVal)
{
$("body").append("<select style=’display:none’ id=’"+parent+child+"’></select>");
$(‘#’+parent+child).html($("#"+child+" option"));

var parentValue = $(‘#’+parent).attr(‘value’);
$(‘#’+child).html($("#"+parent+child+" .sub_"+parentValue).clone());

childVal = (typeof childVal == "undefined")? "" : childVal ;
$("#"+child).val(childVal).attr(‘selected’,’selected’);

$(‘#’+parent).change(function(){
var parentValue = $(‘#’+parent).attr(‘value’);
$(‘#’+child).html($("#"+parent+child+" .sub_"+parentValue).clone());
if(isSubselectOptional) $(‘#’+child).prepend("<option value=’none’ selected=’selected’> — Select — </option>");

$(‘#’+child).trigger("change");
$(‘#’+child).focus();
});
}
[/javascript]

And initialize the association on ‘$(document).ready’, as following example:

<span style="color: #606060"> 1:</span> $(document).ready(<span style="color: #0000ff">function</span>()
<span style="color: #606060"> 2:</span> {
<span style="color: #606060"> 3:</span> makeSublist(&#x2019;parentID&#x2019;,<span style="color: #006080">'childID&#x2019;, true, '</span>selected_val_of_child');
<span style="color: #606060"> 4:</span> });

If you want to create multilevel association, start the initialization from child most order. such as:

<span style="color: #606060"> 1:</span> $(document).ready(<span style="color: #0000ff">function</span>()
<span style="color: #606060"> 2:</span> {
<span style="color: #606060"> 3:</span> makeSublist(<span style="color: #006080">'child'</span>,<span style="color: #006080">'grandson'</span>, <span style="color: #0000ff">true</span>, <span style="color: #006080">''</span>); 
<span style="color: #606060"> 4:</span> makeSublist(<span style="color: #006080">'parent'</span>,<span style="color: #006080">'child'</span>, <span style="color: #0000ff">false</span>, <span style="color: #006080">'3'</span>); 
<span style="color: #606060"> 5:</span> });

For details instruction and example of using this script, please visit the previous version. Feel free to ask me if any problem with using this script or any bug you found.

UPDATE [09/10/09] : Updated function added for jquery 1.3.x

Ajax-like Unobtrusive File Upload

We use AJAX whenever we need to communicate with the server, without reloading the page. But this doesn’t function in the case of File Upload. In usual view, uploading files with AJAX is impossible! (FireFox/Mozilla can do this after change a setting in “about:config” – which we cannot guarantee for a lot of users).

When I was searching for any technique to upload file without reloading page, I found a script in www.webtoolkit.info . But it seemed very complex to me. Then I try to simplify their technique and its working great. So I thought why not share the technique with everybody?

First, look at the demo here to see how it works.

OK. Let’s get into the deep. What happens when we submit a form? The form is submitted to the action page and that page loaded in current frame of window. If the “target” attribute of form indicate “_blank” or any frame name of the window, the page loads in a blank window or in the named frame or inner frame. Here we’ll be using the second method.

First, take an inner frame in anywhere of the page. And set the display property of this frame as “none”:

    <iframe name="hiddenFrame" id="hiddenFrame" 
    src="about:blank" style="display:none" >
    </iframe> 

Now, just set this frame as the target of your uploading form.

<form name="testForm" id="testForm" enctype="multipart/form-data" 
    action="upload.php" method="post" target="hiddenFrame">
        <input type="file" name="name" /> 
        <input type="submit" value="Upload!" />
    </form>

If we submit this form now, it will submit the file to the target script on the server (can be in php, asp etc.). The script will upload the file and will load the response in the frame “hiddenFrame”. As the frame is not visible, nothing about this loading will be seen by the visitors. Now, as the uploading page displays nothing after uploading your file, the response can be immediate. Although it’s not AJAX, this technique will upload your file without (visually) reloading page.

What more?

For many purpose, we might need a callback function when using AJAX. For this file uploading technique also, we will try to create something like that. First, we’ll write the callback function as a simple JavaScript function in the header of the uploading page:

<script language="javascript">
function trackThis()
{
var resultDiv = 
    window.frames.hiddenFrame.document.getElementById(‘result’);
var uploadedImage = document.getElementById(‘UploadedImage’);

uploadedImage.src = resultDiv.innerHTML; 
uploadedImage.style.display="block";

alert("File Uploaded :" + resultDiv.innerHTML);
}
</script>

When the uploading of the server page is complete, it should print the JavaScript code in the hidden iframe, to call the callback function with the reference “window.top”. Any other required information also can be printed in specific DIVs. It will enable us to retrieve any response information using the innerHTML of any specific DIV (as shown in example callback function).

Example:

<?php
print_r($_FILES["name"]);
if(move_uploaded_file($_FILES["name"]["tmp_name"], 
            dirname(__FILE__)."/".$_FILES[‘name’][‘name’]))
{
echo "Your uploaded file is: <div id=’result’>".$_FILES[‘name’][‘name’]." </div>";
}           
else
{
    echo "<div id=’result’>Sorry! Error occured!</div>";  
}
echo <<<test
<script type="text/javascript">
window.top.trackThis()
</script>
test;
?>

Well, that’s it. This technique has already saved my ass once, see if it can save yours also when you need.

jQuery controlled Dependent (or Cascading) Select List

When I was searching the web for a client side dependent list boxes, I got some scripts. But some of them were very much static (static select name, option name etc.), some were vary complex. Then I thought to make it myself.
I have been using jQuery as the standard JavaScript library for most of my web project for some days. Writing javascript coding in jQuery is fun. So here goes my contribution using it.

Here is a simple demo.

Let me guide you through it.

1. Include jqury.js.

<script language=”javascript” src=”jquery.js”></script>

You can get Jquery from here.

2. Write this simple function in a javascript block in head.

function makeSublist(parent,child,isSubselectOptional)
{
$(“body”).append(“<select style=’display:none’ id='”+parent+child+”‘></select>”);
$(‘#’+parent+child).html($(“#”+child+” option”));
$(‘#’+child).html(“<option> — </option>”);
$(‘#’+parent).change(
function()
{
var parentValue = $(‘#’+parent).attr(‘value’);
$(‘#’+child).html($(“#”+parent+child+” .sub_”+parentValue).clone());
if(isSubselectOptional) $(‘#’+child).prepend(“<option> — Select — </option>”);
}
);
}

This function takes 3 arguments: the parent select input’s id, the child select input’s id, and a boolean value to indicate whether to select an item from child list by default.

Example: makeSublist(‘listA’,’listB’,false);

This function will make the options of ‘listB’ list depending on the selection of ‘listA’. And user have to select an item from ‘listB’.
Here ‘listA’ and ‘listB’ are the IDs of parent and child select input respectively.

3. Add a class to the ‘<option>’s of the child list box. The class name will be the value of it’s parent ‘<option>’ in parent listbox with a ‘sub_’ prefix.
Suppose, in parent listbox there is an item “Flower”. Its value is ‘fl':

<option value=’fl’>Flower</option>

When the item “Flower” will be selected, only 3 items should be visible in child listbox. These 3 items should hold the class name ‘sub_fl':

<option class=”sub_fl” value=”1″>Rose</option>
<option class=”sub_fl” value=”2″>Sunflower</option>
<option class=”sub_fl” value=”3″>Orchid</option>

4. Now you are ready. On ‘$(document).ready’ event of Jquery, run the function to associate your list boxes.

$(document).ready(function()
{
makeSublist(‘parentID’,’childID’, false);
});

5. Enjoy :)