Tuesday 14 October 2014

Website theme change using JQuery



For sites which dealing with multiple brands/organizations within it, we can style the site with different themes according to each brand/organization.
 For example, Volkswagen is an automobile group which makes cars in different brands like Audi, Skoda, Lamborghini etc.
In the Volkswagen site we can change the site theme for Audi/Skoda/Lamborghini using a simple click using JavaScript.
As we are using JavaScript we can avoid page reload, which will be fast and user friendly.
For making this we need different css files for each themes.
In our example above, let us assume the css file of Audi is audi.css
And on the theme change button of Audi, call the function … onClick=”SwitchStyle(audi);”

The Theme switch script is given below,
//THEME SWITCH
var styleCookieName = "SelectedBrand";
var styleCookieDuration = 1;
$(document).ready(function () {
    SetStyleFromCookie();  // set theme selected on page load
});

//SWITCH THEME STYLE ACCORDING TO BRAND
function SwitchStyle(cssTitle) {
    $("#theme").attr("href", "css/" + cssTitle + ".css");
    SetCookie(styleCookieName, cssTitle, styleCookieDuration);
    return false;
}

//REMEMBER CURRENT THEME UNTIL LOGOUT
function SetStyleFromCookie() {
    var cssTitle = GetCookie(styleCookieName);
    var keys = cssTitle.split(',');
    var cssName = keys[0];
    if (cssName) {
        if (cssName.length) {
            SwitchStyle(cssName);
        }
    }
}

//GET CURRENT THEME FROM COOKIE
function GetCookie(cookieName) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == cookieName) {
            return unescape(y);
        }
    }
}

//SET SELECTED THEME TO COOKIE
function SetCookie(cookieName, cookieValue, styleCookieDuration) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + styleCookieDuration);
    var c_value = escape(cookieValue) + ((styleCookieDuration == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = cookieName + "=" + c_value;
}

for changing the theme we need to give id="theme" for the default theme link in masterpage.
<link id="theme" href="css/default.css" rel="stylesheet" type="text/css" />
If we want to remember the theme we last selected we can give duration for the cookie. Or else just clear the “SelectedBrand” cookie in logout function.

Smart Light

I would like to share some features about the smart light technology which I think will be the next trending thing in the tech world. It is not a new topic, but by the emergence of smart phones, smart light business got new wings.
Smart light technology is introduced to save energy by turning off light when not needed. The smart light technology mainly used the combination of Bluetooth and WIFI .The smart light can be connected just like ordinary lights into the socket. Companies like emberlight has now started to produce smart light adapters so that any kind of ordinary lights i.e. incandescent, halogens, CFL, LED etc. can be fitted into that socket and convert it to a smart light.
The smart light package has a hardware part and a software part .The hardware part is the light or adapter. The software part is the app which can run using your smart phone The app and light is connected using home Wi-Fi.
These are some of the interesting feature of smart light.
• Switch on or off a single or multiple lights right from your smart phones. • Dim Your Lights automatically based on proximity. • Automatically Dim/Turn off lights in the morning. • Turn Lights on from a distance before returning home. • Know what lights are on in a single room or the whole house. • Know how much unit of current used by each of the light in a month. • Turn your lights on/Off from anywhere in the world. • Automatically turn on the light when you enter your room, turn off when you leave the room. • Set the bed lamp to turn on when you get a call at night.
To know more check this link,