0

I am trying to inject a JavaScript through a Chrome extension, but the script onload method is not called...

var scriptURL = 'http://'+response.scriptUrl,
    scriptTag = document.createElement('script');

scriptTag.type = 'text/javascript';
scriptTag.src = scriptURL;

function onload()
{
    alert('onload');
};
        
scriptTag.onload = onload;
$('head').append(scriptTag);

Any ideas why the alert isn't raised?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Meytaln
  • 15
  • 2
  • Do you have "webRequest" set in your manifest permissions? http://stackoverflow.com/questions/12275998/chrome-extension-cannot-load-external-javascript-via-manifest – Darin Feb 06 '13 at 15:12
  • i did, but it didn't help... – Meytaln Feb 07 '13 at 11:12

1 Answers1

0

Please try this:

var scriptURL = 'http://'+response.scriptUrl,
    scriptTag = document.createElement('script');

scriptTag.type = 'text/javascript';
scriptTag.src = scriptURL;

function onload()
{
    alert('onload');
};

scriptTag.onload = onload;
var head = document.getElementsByTagName('head')[0];
head.appendChild(scriptTag);
belykh
  • 1,109
  • 10
  • 25
  • Thanks for the answer! :) Now that the event is called, i try to access one of the functions inside the script, but it doesn't recognize it... do you have an idea why? I expect the script to be fully loaded and ready for use on the "onload" cb. – Meytaln Feb 07 '13 at 11:28