Take the 2-minute tour ×

Currently I am using jQuery to choose what my index page should load depending whether a cookie is set.

For example, the body tag of the index.html should load hasCookie.html if the cookie is set and noCookieSet.html if no cookie is set.

My index page currently has:

load user scripts
load styling scripts


The user script has the code that checks whether the cookie is set. If it is, I use jQuery's load to load the content into the body:


However, since this is executed asynchronously, the styling scripts have loaded before the body tag has been updated, hence the body is not styled.

Is there a way of having a synchronous load, or should am I approaching this in the wrong way?

EDIT: if it helps, the styling scripts are basically jQueryUI

share|improve this question
The best answer here is don't -- Instead use the callback for .load() to execute your styling functions... –  gnarf Mar 19 '11 at 18:59
add comment

6 Answers

up vote 2 down vote accepted

In your case, this sounds like it would really be a better idea to implement server-side if possible. In PHP it would be a simple matter of detecting if the cookie you want is set in the $_COOKIE array and outputting the correct page.

share|improve this answer
add comment

AFAIK, JQuery load cannot be synchronous.

You can cet around this witha callback function.

$(document.body).load('hasCookie.html', function(){
//call styling script here
share|improve this answer
add comment

You can form it as an AJAX request and populate your page with the response. Set the async option to false. This SO answer has more: How can I get jQuery to perform a synchronous, rather than asynchronous, AJAX request?

share|improve this answer
add comment

Just change document.location.href to the url of the page.

What you're doing is loading content with ajax and placing it within the page body. You'd do this if you didn't want any resources declared in head to get requested. But there's no point in that since you can do it with caching.

share|improve this answer
add comment


$(document).ready(function() { $(document.body).load('hasCookie.html'); });

This will postpone loading until after everything else is already there.

share|improve this answer
add comment

This is how i do it.

You can attach Style and Javascript upon callback from the Ajax Function.

    url: somePage,

                        var script = somePage + '.js';

                        //ATTACH CORRESPONDING STYLE SHEET
                        var style = document.createElement('link');
                        style.type = 'text/css';
                        style.href = '/css/'+somePage+'.css';
                        style.rel = 'stylesheet';
                        style.media = 'screen';


                       //ADD HTML RETURNED


This allows everything to be loaded including styles and javascript, given that the script name and css file name are the same.

share|improve this answer
add comment

Your Answer


