'jQuery cookies : change body id while page loading

How can we change body ID while page loading?

I have just developed an application to change body id which working fine. I would like to detect body ID while page loading which I have chose already.

Here is the code for making cookies, it is working fine

$("#line_view").click(function(){
    $("#box_lev").attr('id','horiz_lev');       
    $.cookie("view_method","horiz_lev", {expires: 365, path: '/'});
});
$("#box_view").click(function(){
    $("#horiz_lev").attr('id','box_lev');
    $.cookie("view_method","box_lev", {expires: 365, path: '/'});
    $("#open").trigger('click');
});

I would to add id while page loading.

Here's my wrong code for that:

if($.cookie("view_method")){
     $("body").attr('id',$.cookie("view_method"));      
}


Solution 1:[1]

if your cookie does have a value, and you just want to trigger it on page load, try this:

$(function(){
    if($.cookie("view_method")){
        $("body").attr('id',$.cookie("view_method"));      
    }
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Joseph