0

This function returns an undefined value when it is executed. I want it to return true if the page is loaded and false if not:

function JLoad (url) {
        var uri = url + ' #div';
        $("#div").load(uri, {'bd': '1'}, function(response, status, xhr){
            if (status != "error"){
                if (window.history && window.history.pushState)
                {
                    window.history.pushState({}, 'Test', url);
                }
                else
                {
                    window.location.hash='!/'+url;
                }
                return true;
            }else{
                return false;
            }
        });
    };

This is part of the code used to request the function:

$(document).ready(function() {
$("a").on("click", function() {
        var url = $(this).attr("href").replace('./', '');
    console.log(JLoad(url));
        return false;
    });
});
7
  • 8
    The method load is Asynchronous, you can not return a value from the callback! Commented Jun 6, 2013 at 19:26
  • and you missed ; in var uri = url + ' #div' Commented Jun 6, 2013 at 19:27
  • @epascarello, could i replace the returns with something like "returnVal = true" and the put "return returnVal" at the end of the function? Commented Jun 6, 2013 at 19:28
  • 1
    What are you trying to accomplish with the returned Boolean. Based on that we can give you an answer that will help out. Commented Jun 6, 2013 at 19:31
  • @epascarello I wish to apply a selected class to the link if the content is successfully loaded, with this code: if(JLoad(url)){ $("a").removeClass("active");$(this).addClass("active");} Commented Jun 6, 2013 at 19:33

1 Answer 1

3

Because jQuery.load() is asynchronous, rewrite your function to accept a callback.

function JLoad (url, cb) {
    var uri = url + ' #div'
    $("#div").load(uri, {'bd': '1'}, function(response, status, xhr){
        if (status != "error"){
            if (window.history && window.history.pushState)
            {
                window.history.pushState({}, 'Test', url);
            }
            else
            {
                window.location.hash='!/'+url;
            }
            cb(true);
        } else{
            cb(false);
        }
    });
}

Then:

$(document).ready(function() {
    $("a").on("click", function() {
        var $link = $(this),
            url = $link.attr("href").replace('./', '');

        JLoad(url, function(successful) {
            if (successful) {
                $("a").removeClass("active");
                $link.addClass("active");
            }
        });
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

I was just about to say, I had to add "var $link = $(this);". But thank you so much for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.