0

I have a JavaScript function that returns an object. I store that object in myarray object and try to return the matched value using switch . it gives me incorrect results when I return the matched value i.e "undefined"

please read the below code and its comments

parseobjectarray('description') // "undefined"


function parseobjectarray(attribute) {
    var returnval;
    $(document).ready(function () {
        var myArray = new Object(); 
        myArray = ParsePagetags(); // returns an object
        switch (attribute) {
            case 'description':
                returnval = myArray.description;
                //alert(returnval); // shows correct result
                break;
            default:
                returnval = "";
        }
            //alert(returnval); // shows correct result
    });
    alert(returnval); // shows incorrect result i.e "undefined"
    return returnval; // shows incorrect result i.e "undefined"
}

WHY???

2
  • 2
    You are running the alert before the document is ready. $(document).ready fires after the page load while your alert happens immediately. Commented Jun 27, 2012 at 20:37
  • i have put the "alert(returnval)" "return returnval" both inside the "$(document).ready(function () {" the "alert(returnval)" shows fine but "alert(parseobjectarray('description'))" return undefined ??? Commented Jun 27, 2012 at 20:55

4 Answers 4

2

Your:

alert(returnval);

is not within the:

$(document).ready(function () {

});

try:

$(document).ready(function () {

    function parseobjectarray(attribute) {

        var returnval;
        var myArray = new Object();
        myArray = ParsePagetags(); // returns an object array

        switch (attribute) {
            case 'description':
                returnval = myArray.description;
                alert(returnval); // shows correct result
            break;
            default:
                returnval = "";
        }

        alert(returnval); // shows correct result
        return returnval;
    }

    parseobjectarray(yourAttribute);

});
Sign up to request clarification or add additional context in comments.

1 Comment

i have put the "alert(returnval)" "return returnval" both inside the "$(document).ready(function () {" the "alert(returnval)" shows fine but "alert(parseobjectarray('description'))" returns undefined ???
1

Your method inside $(document).ready() runs AFTER the alert(returnVal) does.

2 Comments

i have put the "alert(returnval)" "return returnval" both inside the "$(document).ready(function () {" the "alert(returnval)" shows fine but "alert(parseobjectarray('description'))" return undefined ???
Yeah, because the method inside of $(document).ready() runs after parseobjectarray() finishes.
1

Your architecture here is completely wrong. You are initializing a variable, then calling the $(document).ready() method, trying to alert the returnval variable and then return the variable.

Try the following structure:

function parseobjectarray(attribute) {  
    var returnval;  

    var myArray = new Object();
    myArray = ParsePagetags(); // returns an object
    switch (attribute) {
        case 'description':
            returnval = myArray.description;
            alert(returnval); // shows correct result
            break;
        default:
            returnval = "";
    }
    alert(returnval); // shows correct result  

    return returnval;
}

$(document).ready(function(){
    parseobjectarray(attribute);
});

$(document).ready() is used to establish event listeners or fire specified functions once the document has been loaded. If you wish to fire this function once the document is loaded and the function is expected to be reused, then move the function declaration from within the method and then call the function separately.

If the function won't be reused then you can keep the function declaration within the $(document).ready() function.

3 Comments

The reason I use the document.ready function is because I want to load when all the html is loaded? Now I have removed "$(document).ready(function () {" now the "alert(returnval)" and "alert(parseobjectarray('description'))" shows undefined
@KinnanNawaz -- What does ParsePagetags() return? You state an object but what are the properties of that object?
return metatag(name) as obj prop and metatag content as obj value function ParsePagetags() { var myArray = new Object(); $(document).ready(function () { $(parent.document).find('meta').each(function () { var content = $(this).attr("content"); if ($(this).attr("property")) { var prop = $(this).attr("property"); myArray[prop.split(':')[1]] = content; } else { var prop = $(this).attr("name"); myArray[prop] = content; } }) }); return myArray; }
0

Is there a special reason to have $(document).ready(... inside your function ? Try without document.ready...

function parseobjectarray(attribute) {
var returnval;

    var myArray = new Object();
    myArray = ParsePagetags(); // returns an object array
    switch (attribute) {
        case 'description':
            returnval = myArray.description;
            alert(returnval); // shows correct result
            break;
        default:
            returnval = "";
    }

alert(returnval); // shows incorrect result i.e "undefined"
return returnval;
}

1 Comment

The reason I use the document.ready function is because I want to load when all the html is loaded? Now I have removed "$(document).ready(function () {" now the "alert(returnval)" and "alert(parseobjectarray('description'))" shows undefined

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.