未加载通过chrome扩展注入的javascript

时间:2013-02-06 09:08:17

标签: javascript-events google-chrome-extension appendchild inject

我正在尝试通过Chrome扩展程序注入javascript,但是脚本onload方法没有被调用...

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);

为什么不提出警报的任何想法?

1 个答案:

答案 0 :(得分:0)

请试试这个:

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);