Sunday, January 8, 2012

Tag hinting or label hinting javascript library

Tag hinting or label hinting is a very useful thing for forums and blogs, where it is used to tag post.
 
tag-hinting.js is a Javascript library for embedding tah hinting to a webpage, it is very useful on blogs and forums where you tag post. I have created it in such a way that it is easy to use and understand. All you have to do is include the tag-hinting.js file in your webpage and write the following lines:
arr = ['your','tag','values'];
and a div to show the hints.

The function which is to be called is:
hint(this, event)
Note:
Other style values can be added to above div.
id of the div has to be hintwords.
arr has to be declared and should be after file inclusion.
hint take only the specified arguments.
hint can work on keypress, keyup and keydown events.

There is a example file on this in repository on git. Pretty soon i'll be sharing a library which interacts with database and can be helpful in searches.

Friday, January 6, 2012

Javascript Library for dynamic element creation

d-element.js is a javascript library I have created. With the help of it you can easily create dynamic elements, in just one line. Try it!

Required: d-element.js

Using:

$('div').addelement('p',{'background-color':'blue'},'hi');


Explanation:
     This has identical syntax as of jQuery. 'div' here is selector which selects all the div tags.
     addelement method takes three arguments of which first is required and null can be given in other two.

Note:
    Only element(ex: div), id(ex: .id), class(ex: #class) can be used as selector for now, element under which element has to be created.
    First argument of addelement is required which is the element to be created.
    Second argument if given should be associative array, it is for the attributes of the element to be created.
    Third argument is data, if any string to be inserted. It can be a string having other html elements.
    Check console for error logs.

Wednesday, January 4, 2012

jQuery plugin - cycle: Slideshow of your own in a sec

The jQuery Cycle Plugin is a slideshow plugin that supports many different types of transition effects. It supports pause-on-hover, auto-stop, auto-fit, before/after callbacks, click triggers and much more.


Requires: Javascript, jquery


Using:
 


Explanation:
          Line 12: it means check if document is ready to apply javascript over it.
          Line 13: $('#classObject') selects the object having class name classObject. # is called selector. We apply a method cycle over this class.
          Line 14: fx(function/effect) is required parameter which can be defined in many ways here it is shuffle.
          Line 15: shuffle is optional, if its not defined there is default value top:15px and left: width of container. Here both are defined in line 16 and 17.
         Line 19: easing can only be used with easing plugin of jquery.
         Line 20: delay is also required parameter, which takes time for effect for single transition in milliseconds.

Note:
         Take care of the brackets people usually get it wrong. 
         "id" can also be used instead of class (instead of #(hash) .(dot) is used as selector). 
         jquery.js is required.
         jquery easing plugin will make it great.
         Give 3-4 div same class name, by this way there will equal number of div having slideshow of there own. Like if you need multiple slides at a time slideshow.
         There are many other cool fx. You can also create custom.
         There are also some other parameter apart from fx, delay etc. Take a look into the coding of jquery.cycle for other parameters.


There some great demos on the site of jQuery cycle. You can also find easing plugin on this site.

Tuesday, January 3, 2012

MySQL: Alternate to INNER JOIN

I came across a good way to write queries in mysql. Instead of using INNER JOIN there is a easy way to do it, sort of renaming the tables while writing query. Below is the example:

Requires: MySQL

Using:
table1(
   id int,
   name varchar(30)
   age int)
table2(
   id int,
   address varchar(300))

SELECT a.name, a.age, b.address FROM table1 a, table2 b WHERE a.id = b.id;


Explanation:
Look at the query renaming of table is done before retrieving the information we want. WHERE clause is very important in the query because there should be something which is common in both the tables in order to fetch the data.

Note: You can also write queries instead of table name in FROM clause i.e., sub-queries.
          If you are not getting results as you expect, there is something wrong with the way query has been written. Try writing it some other way. And this will happen a lot with complex queries. :P

So, this method will make your queries really powerful and easy. Time to replace INNER JOIN.

Saturday, December 3, 2011

Nodejs - Web's new weapon!

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

This is new thing in town and will be finest armor in your armory. It is easy to learn as it is almost same as Javascript and Python. There is a sample video tutorial on Nodejs site, which tells how easy it is to create a chat server. Once you will learn I can bet you will feel like you can do anything now.

There are several APIs based on it like Pusher, Platform as a Service Nodester and you will find many more things upon Google it. I will come with some good tutorial for it in some time later. Explore it and let me know if you find something interesting based on it.

Javascript Prototype

Javascript prototype is very useful thing. Say, we want to perform some sort of transformation or something else on string  like we want to find some pattern matching for validation. Example below is based on this.


Requires: Javascript

Using:


Explanation:
   Line 2: We created a prototype object for string named found and equate it to null.
   Then in the code below it we find the pattern 'str' insensitive of case.

   Line 3: We create str as String.

   Line 17: Then we gave value to 'found' which can be used further.

Note: Line 3 is important here as prototype for String is given, so str = 'my string' will not work. We have to declare it as String type. Same is to be done if it is being used with other type or custom types.

JSON - Javascript Object Notation

There were days when we used to create and use XML for transporting and store data and it was good at it. But I consider there was a problem with it getting data from it, I feel nausea. I used 'was' here because I also consider it as the way our daddies used to transfer data. Now is the day of JSON.

JSON is cool, easy, requires less memory(though we are not bothered). You can use it with any language and I love it. Frankly, I gave up XML before learning it because of JSON.

JSON looks like array we make in C\C++ or any other language. As we are familiar with the array, it makes understanding of JSON easy.

Syntax: {'data' : ['J','S','O','N'], 'this' : { 'is' : 'json' } }

Let's use it with PHP and Javascript.

PHP:
  1. $array = new array('data'=>array('J','S','O','N'), 'this'=>'json');
  2. $json = json_encode($array);
  3. $json = json_decode($json);
  4. echo $json->data[0];                             //output: J
  5. echo $json->this;                                   //output: json
Javascript:
  1. var json = {'data' : ['J','S','O','N'], 'this' : { 'is' : 'json' } };
  2. alert(json['data'][0]);                            //output: J
  3. alert(json->data[0]);                            //output: J
  4. alert(json->this);                                  //output: json
  5. alert(json['this']);                                  //output: json
We have studying here for PHP and Javscript as it is used mostly for web development. Go to json.org for better understanding and using with other languages.