Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

The title is the best way I could sum it up. It's a little fuzzy on how to explain this. Basically, if I include the javascript file holding the JSON function from within an html file, then trigger the JSON function - callback works. However, if I just call the JSON function from within it's own file - callback never fires. Not sure if this is some sort of javascript or web browser security feature. Would be greatful for an explanation.

Here are some examples.

Working version:

json.html: (trimmed down)

<html>
<head><script type="text/javascript" src="json.js"></script></head>
<script>
    JSON_Object(json_url, function(json_obj) {
        alert(json_obj); // this version works!
    });
</script>
<html>

json.js:

function JSON_Object(json_url, callback) {
    // This function will return an object to the JSON data.
    json_url = json_url + "&callback=jsonCallback";

    var json_script = document.createElement("script");
    json_script.setAttribute("src", json_url);
    json_script.setAttribute("type", "text/javascript");

    window.jsonCallback = function(jsonObjReturn) {
        // cleanup
        document.body.removeChild(json_script);
        delete window[callback];

        callback(jsonObjReturn); // use our callback function to return the data.
    }

    document.body.appendChild(json_script);
}

Non-Working version - Another function inside json.js:

    JSON_Object(content_page, function(json_obj) {
        alert(json_obj); // This version doesn't work. Never called.
    });
share|improve this question
Any errors? This line: delete window[callback];. You probably wanted delete window.jsonCallback;. Shouldn't matter though. – freakish Jun 27 '12 at 6:38
Actually, now that I check. I do get an error on the json_url.php. Error: "Uncaught ReferenceError: jsonCallback is not defined" I am using Chrome and seen that in the Element View. It should be noted that in the case it doesn't work the script is a UserScript loaded by Chrome extensions. I'm assuming this has something to do with it. Possibly scoping of some sort. – Levi Roberts Jun 27 '12 at 7:22
Give us some more info. How exactly the script is being loaded and what is the role of the extension? Generally extensions and background pages are separated. Thus window variable is something else in background and something else in extension. – freakish Jun 27 '12 at 8:02
Sure, no problem. I just wasn't aware that it mattered. Okay, lets try to explain this. As my earlier comment said, this is a javascript file that is a UserScript loaded in Chrome under development mode. This file is built to affect a webpage without actually being hosted on the server. IE: To modify the looks of a webpage like any other extension/UserScript. The role is to retrieve data from another server (my server) using JSON. It would make sense that the window variable is scoped. I assume that the window.jsonCallback function isn't available to the file when loaded as a UserScript. – Levi Roberts Jun 27 '12 at 17:46

1 Answer

After a bit of research, I found that I was right. It IS a web browser security feature for extensions. From the following link: http://code.google.com/chrome/extensions/content_scripts.html

Quote from the page

However, content scripts have some limitations. They cannot:

  • Use variables or functions defined by their extension's pages
  • Use variables or functions defined by web pages or by other content scripts

JSON falls into the above categories because it uses a function for it's callback.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.