Popular JavaScripts which makes the web more richer and interactive

October 3rd, 2009 by Sunil

Now a days, JavaScript is unavoidable for any website. JavaScript enriches your website in the form of giving more interactivity to your web page and help to dressing up your web page.

The use of JavaScript can be listed like this

  1. Checking and correcting the form data in the user side
  2. Dynamically change the web page without reload it.
  3. Create applications like calculator, simple applications and games.
  4. Add effects to a web page which will attract more visitors

These are the important use of JavaScript. Some of them are very important, some of them not that much. Here I am showcasing some important JavaScript tools which commonly using in website.
Read the rest of this entry

Collapsing and expanding footer using JQuery

September 24th, 2009 by Sunil

Footer concept changing day by day. Web2 footer is big and lot of links and information carrying on that. Here you can see different types of footers.

Today I am going to discuss about collapsing and expanding footers. Most of the site doesn’t have collapsing and expanding footer. But It would be nice if you have one like that. Because it is easy for the user. If he wants to see that he can otherwise not. It avoids the unnecessary scroll.

Read the rest of this entry

Different ways to include jQuery in your website

May 30th, 2009 by Sunil

jQuery is javascript frame work which simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development.

You can include jQuery in different ways. 

1.Download it from jQuery site and include it into your webpage

1
<script src="javascript/jquery-1.3.2.min.js" type="text/javascript"></script>

2.Include it from Google server

1
2
3
4
5
6
7
8
<script src="https://www.google.com/jsapi"></script>
<script>
google.load('jquery', '1.3.1');
</script>
 
or
 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>

3.Include it from Jquery’s site 

1
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>

These are the different methods to include jQuery. Each of them have there own advantages and disadvantages.

Read the rest of this entry

Find the biggest from the unordered list or ordered list

April 13th, 2009 by Sunil

Yesterday I came around the situation to find the biggest li from the list of unordered list (UL). I have done that. And added a class which is the bigger in the list. Same can be apply to the ordered list (OL) too. I think it will be helpful for the others too. It is using jQuery as javascript framework. 

If you have any questions or alternate solutions please let me know.

The code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <title>Find the biggest from the unordered list or ordered list</title>
<script type='text/javascript' src='jquery-min.js'></script>
<script type="text/javascript">
    $(document).ready(function() {
		len=$("#list li").length;
		bigger=0;
		for(i=0;i<len;i++)
		{
			val1 = $("#list li").eq(i).text().length;
			if(val1 > bigger)
			{
				bigger = val1;
				pos =  i;
			}
			else
			{
				bigger = bigger;
 
			}
		}
	alert("postion "+ (pos + 1) + " is the biggest with "  +bigger+ " characters");
	$("#list li").eq(pos).addClass('bigger');
});
</script>
</head>
<body>
    <ul id="list">
	    <li>123</li>
		<li>1234</li>
		<li>123456789</li>
		<li>12345</li>
 
	</ul>
</body>
</html>

Download the source

How to make a colour scheme for your website using JQuery and CSS

January 17th, 2009 by Sunil

It is an interesting thing that if we see some changes on a website, both content wise and layout or colour wise. So I decided to add a colour scheme to my blog.So the visitors can select a colour scheme from these buttons. Have you notice the 6 buttons in this page on the header portion?

Select colour scheme

First of all I have placed 6 buttons on the header portion of this blog for the visitors to select their choice. viz
1. SeaGreen, 2. Brown, 3.Blue, 4. Orange, 5. Green, 6. Pink.

So the html will look like this.

1
2
3
4
5
6
7
8
<div id="skin-select">
<a href="#" title="SeaGreen" class="seagreen">SeaGreen</a>
<a href="#" title="Brown" class="brown">Brown</a>
<a href="#" class="blue" title="Blue">Blue</a>
<a href="#" title="Orange" class="orange">Orange</a>
<a href="#" title="Green" class="green">Green</a>
<a href="#" title="Pink" class="pink">Pink</a>
</div>

2nd Step
We need to make the changes on CSS file. I have added skin.css to my main css via import statement like @import url(“skin.css”);

In the skin.css I have added the colors where ever I need to change the background color, font color or border.

3rd step
Now come the javascript part. I am using the JQuery frame work. I like Jquery very much. So we need to include the jquery framework to our page and we need to write custom javascript code for its working. For that I added script.js to my page.

In the script.js I added the following code in the document.ready function.
$("#skin-select a").click(function(){
if($("body").attr("class")){
$("body").removeClass($("body").attr("class"));
}$("body").addClass($(this).attr("class"));
});

What this code will do?

When clicking each button it will add a class to body. If the body already has a class, it will remove the existing class and add a new class with new color name.

If your css is right it will work fine on each click.

What is next?

If you close the browser and open the it again with the same url, you can see the default colour scheme only. But I would like to see the colour scheme which I have recently opted.

For that we need to save this information in your computer. I have added a Jquery plugin for saving this colour scheme to the computer. A cookie is information that a Web site puts on your hard disk so that it can remember something about you at a later time. So I have added a jquery plugin for saving cookie.

Now we can save the latest color scheme to our visitor’s machine. So next time when he opens the site he can see the latest colour which he has selected in the last.

So the final code will look like this.
$(document).ready(function() {
if($.cookie('theme_color')!= null)
{
var coockiecolor = $.cookie('theme_color');
$("body").addClass(coockiecolor);
}
$("#skin-select a").click(function(){
if($("body").attr("class")){
$("body").removeClass($("body").attr("class"));
}
$("body").addClass($(this).attr("class"));
var colorname = $(this).attr("class");
$.cookie('theme_color', colorname, { path: '/', expires: 10 });return false;});
});

Inside the document.ready function first it will check whether any cookie saved for theme or not. If it is available it will take the theme information from the cookie. You can check the documentation about jQuery cookie in detail can be find here.

What do you think now? Is it easy? Waiting for your valuable feedback.

Pushup – advice your visitors to upgrade their browser

January 9th, 2009 by Sunil

It is an interesting thing that pushup javascript will tell the visitors that they have to upgrade their browsers to the  latest one if they are using the  old browsers.

It is easy to install and customisable

Upload the pushup package in to your server
Include the pushup.css and pushup.js in to your page header.

Pushup shows warning in firefox

Pushup shows warning in firefox2

Pushup shows warning in ie6

Pushup shows warning in ie6

If you want to upload this package other than the js directory, change your css background image path accordingly. More information available here (http://www.pushuptheweb.com/)

Download latest pushup can be download from here (http://github.com/staaky/pushup/tree/master)

Currently pushup can be integrated with the following frame work

Open links in new window automatically in Wordpress using JQuery

January 6th, 2009 by Sunil

Sometimes we need to open the external links automatically in our websites. Here I am adding a small snippet for that. JQuery is JavaScript framework using for that. So first you need to include Jquery framework to your page.

For that either you need to download the jquery and add it your page, or you can link directly load the framework using Google code

How we can include JQuery using Google code
Check out this link
http://code.google.com/apis/ajaxlibs/documentation/index.html

Benefits of including JQuery from Google code

  1. It saves on bandwidth
  2. It will load quickly from Google’s CDN.
  3. It will already be cached if the user has visited a site which delivers it from Google code.

Ohh.. I am going out of the subject. Ok, After including Jquery you need to write the below code either in your separate Javascript file or your in the html page.
$(document).ready(function() {
$("a[@href^=http]").each(
function(){
if(this.href.indexOf(location.hostname) == -1) {
$(this).attr('target', '_blank');
}})
});

If you want to add a class on external links for styling differently or add a background to it? It is easy. Just add this code. It will add a class to your external links.
$(document).ready(function() {
$("a[@href^=http]").each(
function(){
if(this.href.indexOf(location.hostname) == -1) {
$(this).attr('target', '_blank');
$(this).addClass(‘external’);
}})
});

I am using this code in my blog. It works well. If you find any bug on it, Please let me know.

[update]

Today I added $(this).attr('rel', 'external nofollow');

So Google may not follow any external link. Now the script should look like this


$(document).ready(function() {
$("a[@href^=http]").each(
function(){
if(this.href.indexOf(location.hostname) == -1) {
$(this).attr('target', '_blank');
$(this).attr('rel', 'external nofollow');
}}
)});