我已经获得了这个函数来将外部javascript文件注入渲染的html:
function inject(src, cb, target){
target = target || document.body;
var s = document.createElement('SCRIPT');
s.charset = 'UTF-8';
if(typeof cb === 'function'){
s.onload = function(){
cb(s);
};
s.onreadystatechange = function () {
(/loaded|complete/).test(s.readyState) && cb(s);
};
}
s.src = src;
target.appendChild(s);
return s;
}
这是调用函数:
$(function (){
inject(swifttagurlftlpre43sc,function(script){
var a = swifttagdiv.firstChild.attributes[2].value.substr(11);
var is = swifttagdiv.firstChild.firstChild.src;
var onLine = (is.substring(is.lastIndexOf('/') + 1) == 'staffonline.png');
if (onLine) {
$('#spanactive2').html('<span style="color: #567591;"><?php echo($content->getString('active')); ?><\/span>');
$('#chaton2').html('<img src="/site/img/chaton.gif" alt="" />');
$('#chat_box_timer').click(function() { eval(a); });
} else {
$('#live_chat_block_timer').remove();
$('#chaton2').html('<img src="/site/img/chatoff.png" alt="" />');
}
});
});
它在Firefox上运行良好。但它不适用于chrome。开发人员工具给我这个错误: 未捕获的TypeError:无法调用null
的方法'appendChild'我如何修复,或者至少开始调试?
答案 0 :(得分:0)
我在onload完成之前尝试正确访问正文时遇到了困难(我以为这是一个IE问题,但它可能更普遍)。可能是吗?
答案 1 :(得分:0)
我最好的猜测是,不同浏览器的DOM生成方式不同,swifttagdiv.firstChild在chrome中没有属性[2],但在其他浏览器中也有。它所具有的是属性['href']属性。所以解决方案是改变
var a = swifttagdiv.firstChild.attributes[2].value.substr(11);
到
var a = swifttagdiv.firstChild.attributes['href'].value.substr(11);