7

How to load JSON from a variable, from an object

 3 years ago
source link: https://www.codesd.com/item/how-to-load-json-from-a-variable-from-an-object.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to load JSON from a variable, from an object

advertisements

I have this code:

var browserName = "chrome";

    function getBrowserHack() {

        var browsersStack = {
            "chrome" : {
                "hacks": {
                    "global" : "!!window.chrome;",
                    "version" : {
                        "32" : "!!window.chrome && !!window.chrome.webstore;"
                    }
                }
            },
            "firefox" : {
                "hacks": {
                    "global" : "!!window.sidebar;",
                    "version" : {
                        "2": "(function x(){})[-6]=='x';"
                    }
                }
            },
            "safari" : {
                "hacks" : {
                    "global" : "/constructor/i.test(window.HTMLElement);"
                }
            },
            "opera" : {
                "hacks" : {
                    "global" : "window.opera && window.opera.version() == X;"
                }
            }
        }
        return $.getJSON(browsersStack, function(data){
            var browsers = data[browserName];
            return browsers;
        });
    }

i need to return function with this JSON data, and i have an error [object%20Object] 404 (Not Found). What i'm doing wrong?


I think you misunderstood getJSON, you have the JS Object already right there in your code, try this:

function getBrowserHack(browserName) {

    var browsersStack = {
        "chrome" : {
            "hacks": {
                "global" : "!!window.chrome;",
                "version" : {
                    "32" : "!!window.chrome && !!window.chrome.webstore;"
                }
            }
        },
        "firefox" : {
            "hacks": {
                "global" : "!!window.sidebar;",
                "version" : {
                    "2": "(function x(){})[-6]=='x';"
                }
            }
        },
        "safari" : {
            "hacks" : {
                "global" : "/constructor/i.test(window.HTMLElement);"
            }
        },
        "opera" : {
            "hacks" : {
                "global" : "window.opera && window.opera.version() == X;"
            }
        }
    }
    return browsersStack[browserName];
}

// Now get the browserhack by calling:
var browserHack = getBrowserHack('chrome');


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK