Monday 29 September 2014

Simple Jquery Accordion div for your website

Accordion is expandable and collapsible content holder that is broken into sections and probably looks like tabs. On net you will get plenty of Accordion plug-ins which are with their complicated structure and heavy js files. So they are hard to modify   as per your requirement.



I am writing here a simple and light weight jquery accordion. This is basic structure with scope of modifications. So try this out.

<!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">
<head>
    <title>Accordion</title>
    <script src="http://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $(".accordiondiv h1").click(function () {
                $('.active').removeClass('active');
                $(this).addClass('active');

                $(this).siblings('.accordionbody').slideToggle();


            });

        });
    </script>
    <style>
        .accordionbody
        {
            display: none;
            border: 1px solid;
            padding: 8px;
        }
        .accordiondiv h1
        {
            background-color: #333;
            color: #fff;
            font-weight: normal;
            padding: 10px;
            font-size: 17px;
            margin: 0;
            border: 1px solid;
        }
    </style>
</head>
<body>
    <div class="maindiv">
        <div class="accordiondiv">
            <h1>
                accordion 1</h1>
            <div class="accordionbody">
                some text here
            </div>
        </div>
        <div class="accordiondiv">
            <h1>
                accordion 3</h1>
            <div class="accordionbody">
                some text here
            </div>
        </div>
        <div class="accordiondiv">
            <h1>
                accordion 4</h1>
            <div class="accordionbody">
                some text here
            </div>
        </div>
    </div>
</body>
</html>



Saturday 20 September 2014

Components required for responsive web designing





Responsive web design (RWD) is a web design approach aimed at making websites change itself to provide an optimal viewing experience—easy reading and navigation with a minimum of resizing, panning, and scrolling—across a wide range of devices (from mobile phones to desktop computer monitors).

That means make your website to adjust itself as per viewer's screen resolution or device such as mobile or desktop. In this way you can increasing user experience of your website. In this discussion we are going to take look at components that requires to build responsive website.

1)View port
2)css media queries

View port
View port is very important component for your responsive Website. Without it your website can not show phenomena of responsiveness on mobile devices.

View port actual reads width of device and ask browser to do responsive things. View port lies between head tags of your html code.
<meta name="viewport" content="width=device-width, initial-scale=1.0">


Css3 Media queries

Css3 media queries are introduced to targeting different sizes of devices or view port. Basically this gives you to build css to target specific width's devices. Means you can write style separately for mobile devices, tablets and desktops. In this way website fits to that specific device perfectly. But you should design your website differently for each set of devices.

You can include media queries in three ways to your webpage.
1)In the head tag of html you can import different style sheets for different widths of devices, like this
<link href="styles.css" rel="stylesheet" media="all and (max-width: 1024px)">


2)Or you can import external style sheet inside your existing style sheet
@import url(styles.css) all and (max-width: 1024px) {...}


3)Or you can write rules for different media queries inside your single style sheet
@media all and (max-width: 1024px) {...}

Most experts recommend the 3rd one that means write media queries in side your one single css style sheet. Because importing external style sheets may increase your page request, which may cause slowdown your page to load.

Each media query may include a media type. Common media types include all, screen, print, tv, and braille. The HTML5 specification includes new media types, even including 3d-glasses.Screen is default media type if not specified. Way to write media type is
@media only screen {}

@media only print {}


Style you want to apply for screen media (like desktop, mobile etc) go inside '{}' and same for print media.

For responsive website it is necessary to write different styles for different width of screen. So media query gives you that option. You can write it like this
@media only screen and (min-width : 320px) and (max-width : 480px) {
/* Styles */
}


The style written in this media query will apply for screen with width between 320px to 480px.
We can also write in this manner given below
@media only screen and (min-width : 320px){
/* Styles */
}

This will apply style only for screen with above 320px. Here we are not restricting max screen width.
@media only screen and (max-width : 320px) {
/* Styles */
}

This will apply style for screen with below 320px. That means this will never apply for screen width above 320px

Now like this you can write style for specific conditions you need. You can add as many media queries in style sheet you want.

I hope this will help many beginners in responsive web designing.
I next session I am going to discus recommended breakpoints or screen sizes for media queries.




Sunday 14 September 2014

Redirecting web page onload or delay redirecting

When somebody clicks on link on page to reach specific page, but purposefully developer direct him to another page is called as redirection. Reason behind this may differ for each website.

It can be done in javascript by window.location like this

<script>
window.location="yourlink.com";
</script>

Now it is totally your decision that when you wants to redirect user to that page. If you add above code into the  <head></head>. Then it will perform before your page loaded.

But what if you want it to perform after some delay. You have to do slight addition in this code


<script>
setTimeout(function(){
window.location="yourlink.com";
}, 3000);
</script>

We are using setTimeout for delaying this redirection for 3000 milliseconds i.e 3seconds.
If you want it to be done after page load then try this out

<script>
$(document).ready( function() {
window.location="yourlink.com";
});
</script>


Wednesday 10 September 2014

How to build query string in javascript?






This article will discuss 'How to build query string in javascript?'. Copy and paste this html code in your page's body tag. 
<input id="Txtname" placeholder="Name" name="Txtname" /> <input id="Txtmobile" placeholder="Mobile" name="Txtmobile"/> <input id="Texemail" placeholder="Email Id" name="Texemail" /> <input type="submit" id="submit" title="Submit" value="Submit" onclick="sendm()" />


On button click below code will fetch text from text-boxes, build query string and redirecting to that query string. Here 'buildUrl' is a function that builds & returns query string.
<script type="text/javascript"> function buildUrl(url, parameters) { var qs = ""; for (var key in parameters) { var value = parameters[key]; qs += encodeURIComponent(key) + "=" + encodeURIComponent(value) + "&"; } if (qs.length > 0) { qs = qs.substring(0, qs.length - 1); //chop off last "&" url = url + "?" + qs; } return url; } function sendm() { var url = "Form.aspx"; var parameters = new Array(); parameters["name"] = document.getElementById('Txtname').value; parameters["mobile"] = document.getElementById('Txtmobile').value; parameters["email"] = document.getElementById('Texemail').value; var site = buildUrl(url, parameters); window.location = site; } </script>

Tuesday 2 September 2014

How to make simple Javascript tab with css and html?

In this session we will write a tabs structure with the help of jquery, html & css.

When you are writing some content on your webpage, then it is better practice that showing your content in a tab format than normal text format. This let your page looks more cleaner than normally formatted text.

If you find tabs jquery on google you will get jquery plugins that will force you to keep unnecessarily heavy js and css files on your webpage, which let slowdown your page. In this session we will write a tabs structure with the help of jquery, html & css.




First of all we will write simple html given below. We are using 'ul', 'li' as tabs(tab heading) and below that we will use 'div' for contain your html code. Each div is having id. You can change internal html as per your requirement. Tabs 'li' is having rel properties containing value of  id of targeted div that to be shown.

/************************Html**********************/

<div class="tabcontainer">

<ul class="tabheading">

                    <li class="active" rel="tab1"><a href="javascript:return false;">Tab 1</a> </li>

                    <li rel="tab2"><a href="javascript:return false;">Tab 2</a> </li>
</ul>



<div class="tabbody active" id="tab1" style="display: block;">
Tab 1
Your Text or html

</div>

<div class="tabbody" id="tab2" style="display: none;">
Tab 2
Your Text or html

</div>

</div>


Below is css for our tab. You may do styling of your tab as you want.


/************************Css************************/
.tabcontainer {
float: left;
width: 100%;
}
.tabheading li.active {
background-color: #fff;
border-bottom: 0;
margin-bottom: -1px;
}
.tabheading li {
display: inline-block;
border: 1px solid #ddd;
background-color: #eee;
margin: 0;
}
.tabheading li a { 
padding: 0 2%; 
}
.tabbody.active {
display: block;
}
.tabbody {
border: 1px solid #ddd;
float: left;
min-height: 10px;
width: 96%;
padding: 2%;
display: none;
}


Now main part of our structer that is our Javascript. Copy & paste this small code given below on your webpage at bottom.

/*****************************Js************************/
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

<script>
 $('.tabheading li').click(function () {
        var tabid = $(this).attr("rel");
        $(this).parents('.tabcontainer').find('.active').removeClass('active');
        $('.tabbody').hide();
        $('#' + tabid).show();
        $(this).addClass('active');

        return false;
    });
</script>


In this script on click of tab heading li we are reading id from 'rel' property of it and showing div with that id