我有一些页面需要根据 XML 文件中的数据描述动态加载内容。可以动态加载的项目包括 SWF。我有代码可以通过 http 和文件协议(protocol)在 Firefox 中正确加载和启动电影,并通过 http 协议(protocol)在 Chrome 中正确加载和启动电影。我还需要它以 http 和文件协议(protocol)在 Internet Explorer 中成功加载,但所有 Flash Video Player 报告都是“电影未加载...”。有人可以查看以下信息并给我修复吗?
XML 中 Flash 对象的描述如下所示:
<multimedia
type='flash'
swf='swf/filename_here.swf'
width='600'
height='400'
version='7.0.19.0'
/>
我有 JavaScript 可以解析它并创建一个类似于以下 JSON 的对象:
{
'tag': 'multimedia',
'attributes': [
'type': 'flash',
'swf': 'swf/filename_here.swf',
'width': '600',
'height': '400',
'version': '7.0.19.0'
]
}
最终,这个对象被传递给一个创建 DOM 元素的函数(是的,我知道这个函数的顺序很奇怪;我尝试了不同的方法来让它工作):
var path = var path = document.location.href;
path = path.substr(0, path.lastIndexOf('/') + 1);
var version = null;
function isIE() {
return navigator.userAgent.lastIndexOf('Trident') > 0;
}
function buildFlash(element) {
version = element.attributes.version;
var name = document.createElement('param');
name.setAttribute('name', 'movie');
name.setAttribute('value', path + element.attributes.swf);
(if (!isIE()) {
var inner = document.createElement('object');
inner.setAttribute('type', 'application/x-shockwave-flash');
inner.setAttribute('data', path + element.attributes.swf);
inner.setAttribute('width', element.attributes.width);
inner.setAttribute('height', element.attributes.height);
}
var flash = document.createElement('object');
flash.setAttribute('id', 'flashMovie');
flash.setAttribute('classid', 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000');
flash.setAttribute('width', element.attributes.width);
flash.setAttribute('height', element.attributes.height);
flash.appendChild(name);
if (!isIE()) {
flash.appendChild('inner');
}
var div = document.createElement('div');
div.setAttribute('id', 'multimedia');
div.appendChild('flash');
return div;
}
生成的 div 最终会添加到页面中的正确位置。
有什么想法吗?
最佳答案
IE 不支持动态调整对象元素的大部分属性/参数。
您可以使用此函数创建具有给定属性和参数的跨浏览器对象元素。
var createSwfObject = function(src, attributes, parameters) {
var i, html, div, obj, attr = attributes || {}, param = parameters || {};
attr.type = 'application/x-shockwave-flash';
if (window.ActiveXObject) {
attr.classid = 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000';
param.movie = src;
}
else {
attr.data = src;
}
html = '<object';
for (i in attr) {
html += ' ' + i + '="' + attr[i] + '"';
}
html += '>';
for (i in param) {
html += '<param name="' + i + '" value="' + param[i] + '" />';
}
html += '</object>';
div = document.createElement('div');
div.innerHTML = html;
obj = div.firstChild;
div.removeChild(obj);
return obj;
};
例子
var swfEl = createSwfObject('http://example.com/example.swf', {id: 'myid', 'class': 'myclass', width: 100, height: 100}, {wmode: 'transparent'});
document.body.appendChild(swfEl);
关于JavaScript:如何将 Flash 文件 (SWF) 动态添加到 DOM 以便 IE 加载它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5070432/