使用 JavaScript 将查询参数更新到 Flash 对象

Updating query parameters to Flash object using JavaScript

本文关键字:Flash 对象 更新 参数 JavaScript 查询 使用      更新时间:2023-09-26

我为Flash嵌入了以下简单的对象:

<object id="siwp_obj"
        type="application/x-shockwave-flash" 
        data="button.swf?file=%2F/ex.php?id=89804c7326e92a00c5c88c181190af8159cf060b"
        height="32" width="32">
    <param id="siwp_param" name="movie"
           value="button.swf?file=%2F/ex.php?id=89804c7326e92a00c5c88c181190af8159cf060b" />
</object>

我想做的是将id参数替换为浏览器生成的新 ID。

我已经尝试过这个,它适用于Firefox,但不适用于Chrome或IE9。 Firefox 成功更新了参数,因此 Flash 对象引用了新 ID,但 Chrome 和 IE9 不会更新 ID,也不会导致任何脚本错误或警告。

var cid = "randomstring";
var obj = document.getElementById('siwp_obj');
// get the "data" attribute which contains the URL with an old id to replace
obj.setAttribute('data', obj.getAttribute('data').replace(/[a-zA-Z0-9]{40}$/, cid));
obj = document.getElementById('siwp_param');
obj.value = obj.value.replace(/[a-zA-Z0-9]{40}$/, cid);

我试图假设没有可用的 javascript 库,因为这将在插件系统中使用。

有没有人对如何动态更新对象和参数标签的 URL 有任何想法?

编辑:

在Chrome中,我进行了一些调试,并注意到该参数正在更新,但是当我与闪存交互时,它尚未重新加载。

例如:

var obj = document.getElementById('siwp_obj');
alert(obj.getAttribute('data')); // the old URL with old ID
obj.setAttribute('data', obj.getAttribute('data').replace(/[a-zA-Z0-9]{40}$/, cid));
alert(obj.getAttribute('data')); // the new URL with new ID

。但是当我单击 flash 对象以尝试从新 URL 流式传输时,它仍然引用具有旧 ID 的旧 URL。 因此,DOM 正在更新,但浏览器随后缺少对象的某些参数已更改的事实。 有什么想法吗?

我相信

最好的办法是动态创建整个元素(而不是在加载到页面后将其作为getElementById()来处理。

因此,document.createElement("object");document.createElement("param")然后添加所有属性,然后将其appendChild()到某个父节点...我相信这应该有效(尽管我还没有测试过)。

我能够通过更新 dom 元素,然后创建一个克隆并在文档中替换它来让它工作。

这适用于IE6+,Firefox和Chrome(尚未在任何其他版本中测试过)。

// update object and param tag with new id
var obj = document.getElementById('siwp_obj');
obj.setAttribute('data', obj.getAttribute('data').replace(/[a-zA-Z0-9]{40}$/, cid));
var par = document.getElementById('siwp_param'); // this was a comment...
par.value = par.value.replace(/[a-zA-Z0-9]{40}$/, cid);
// replace old flash w/ new one using new id
// clone the original object tag and then insert the clone, remove the original
var newObj = obj.cloneNode(true);
obj.parentNode.insertBefore(newObj, obj);
obj.parentNode.removeChild(obj);