[英]Internet Explorer 8 and getElementsByTagName('script') not working
Another user suggested a function for converting a string to dom elements then iterating through the string and executing the js. 另一个用户建议使用一个函数,将字符串转换为dom元素,然后遍历字符串并执行js。 It works fine just not in IE8, surprising.
令人惊讶的是,它只能在IE8中正常运行。
function get_script_src_from_string (INPUT_STRING) {
var tempDiv = document.createElement('div');
tempDiv.innerHTML = INPUT_STRING;
var scripts = tempDiv.getElementsByTagName('script');
var script_urls = [];
for (var i=0; i<scripts.length; i++) {
script_urls.push(scripts[i].src);
}
return script_urls;
}
I am simply passing in a test string: 我只是传递一个测试字符串:
get_script_src_from_string('<div class="tester" id="tester"><script id="tester2" type="text/javascript">alert("test");</script></div>');
If I do a test with 如果我用
var test = tempDiv.getElementsByTagName('*');
alert(test.length);
All browsers return 2, ie8 returns 1, so it is not recognizing script as a tag. 所有浏览器均返回2,即8返回1,因此它无法将脚本识别为标记。 Using jquery for this project is not an option unfortunately.
不幸的是,对此项目使用jquery并不是一种选择。
function get_script_src_from_string (INPUT_STRING) {
var frag = document.createDocumentFragment();
var tempDiv = frag.appendChild( document.createElement('div'));
tempDiv.innerHTML = 'X' + INPUT_STRING;
var scripts = tempDiv.getElementsByTagName('script');
var script_urls = [];
for (var i=0; i<scripts.length; i++) {
script_urls.push(scripts[i].src);
}
return script_urls;
}
Adding a character before the input string and creating a doc fragment seems to work on IE8 and all other real browsers. 在IE8和所有其他真正的浏览器上似乎可以在输入字符串之前添加字符并创建文档片段。
According this other post , using innerHTML detaches all Dom nodes for that element, ie the nodes are there but the DOM doesn't update to recognize those nodes. 根据另一篇文章 ,使用innerHTML分离该元素的所有Dom节点,即,这些节点在那里,但是DOM不会更新以识别那些节点。 A solution would be to use appendChild, which should update the DOM and make those elements accessible via getElementsByTagName.
一种解决方案是使用appendChild,它应更新DOM并通过getElementsByTagName使这些元素可访问。
Here is the whole function I am using incase I missed something, check if jquery exists, if not use a native js append. 这是我正在使用的整个功能,以防万一我错过了一些东西,请检查是否存在jquery,如果不使用本机js追加。 TODO: put in a fix for certain tag names besides script like style.
待办事项:除了样式之类的脚本外,还修复了某些标记名称。
_append = function(selector, d) {
if(typeof(selector)=='string') selector =_select(selector);
if(typeof(selector)!='object'||typeof(d)!='string') {
return false;
}
if(typeof(jQuery)=='function') return (jQuery(selector).append(d)||false); // jQuery available
var content = d;
content = Array.prototype.concat( [], content );
if(content.length) {
var frag = document.createDocumentFragment();
var tmp = frag.appendChild( document.createElement('div'));
tmp.innerHTML = 'X' + content;
var scripts = tmp.getElementsByTagName('script');
// Append html.
if(selector) {
selector.insertAdjacentHTML('beforeend', content);
} else {
console.log('Invalid selector provided.');
}
for(var i=0; i<scripts.length; i++) {
var newScript = document.createElement('script');
if(scripts[i].id) {
newScript.id = scripts[i].id;
}
if(scripts[i].src) {
newScript.type = 'text/javascript';
newScript.src = scripts[i].src;
document.getElementsByTagName('head')[0].appendChild(newScript)
} else {
var nscript = document.createElement('script');
var js = scripts[i].innerHTML;
nscript.text = js;
document.getElementsByTagName('head')[0].appendChild( nscript ).parentNode.removeChild( nscript );
}
}
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.