0

I want to get some data and passed to the website.

    window.onload = function(){
        var num = Math.round(10000*Math.random());
        var callbackMethodName = "cb_" + num;

        window[callbackMethodName] = function(data){
            letStart(data);
        }
        var sc = document.createElement("script");
        sc.id = "script_" + callbackMethodName;
        sc.src = "http://example.com/getData.php?callback=" + callbackMethodName;
        document.body.appendChild(sc);      
        document.getElementById(sc.id).remove();        

        function letStart(badges){

            for (var i = 0; i < badges.length; i++){
                console.log(badges[i][0]);
            }

        }

    }

If the above code is execute on html page it will work. But when inject the code I get the following error.

ReferenceError: Can't find variable: cb_4276

Maybe this part dosent work, but how to make it work?

window[callbackMethodName] = function(data){
                letStart(data);
            }

// Edit

Tutorial

5
  • Do you have a working fiddle? I would pinpoint the source of the error at the window[callbackMethodName] line or the sc.src line. Dec 30, 2015 at 18:55
  • What line of code does that error point to? Side note you do not need document.getElementById(sc.id) you already have a reference to the script element: sc, just do sc.remove() Dec 30, 2015 at 18:59
  • I can't get it work there, but copy the javascript code and replace the src and inside put echo $_GET['callback']."(".json_encode($someArray).")";
    – lotch
    Dec 30, 2015 at 18:59
  • Thats all it shows: ReferenceError: Can't find variable: cb_4276 (anonymous function)getData.php:1
    – lotch
    Dec 30, 2015 at 19:01
  • In stand alone file it works, but in the extension it doesnt.
    – lotch
    Dec 30, 2015 at 19:02

0

Your Answer

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

Browse other questions tagged or ask your own question.