簡體   English   中英

使用JavaScript將查詢參數更新為Flash對象

[英]Updating query parameters to Flash object using JavaScript

我為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庫,因為它將在插件系統中使用。

是否有人對如何動態更新對象和param標簽的URL有任何想法?

編輯:

在Chrome中,我進行了一些調試,並注意到該參數正在更新,但是當我與Flash交互時,尚未重新加載該參數。

例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM