使用 javascript 更新 <embed> 参数

Using javascript to update <embed> parameter

我最近一直在尝试实现一个 Flash 应用程序,在某些时候需要通过 html 嵌入。它看起来像 >

<embed src=".." quality=".."  ... and at some point  FlashVars="&firstparam&secondparam..."

我想做的是实现一个下拉菜单,按下时会更改 FlashVars 参数,以便应用程序显示不同的内容。我试过

document.getoElementByID().FlashVars="new parameters"

但它不起作用(它非常适用于 highlited 默认参数,例如 src、高度、宽度...)

我也尝试用 javascript 片段再次编写整个嵌入部分,但它也没有用。 javascript 是如何做到的?我是该领域的初学者,非常感谢任何帮助。

谢谢。

只需这样做:

$('embed') // targets the embed tag in the DOM

.attr("attribute-name","attribute-value");

这是一个例子:https://jsfiddle.net/DinoMyte/1a6mwb13/2/

要了解您的代码为何不起作用,您应该了解什么是 flashvars 参数及其工作原理。

Adobe 说 here,例如:

The FlashVars parameter of the HTML <OBJECT> tag sends variables into the top level of a SWF file when it loads in a web browser. The <OBJECT> tag is used to add SWF files to HTML pages. The <EMBED> tag can also be used, but is older and now obsolete.

所以在这里我们可以理解这些变量是在加载 SWF 时加载的,这就是为什么即使您更改了 flashvars 参数,它也不会做任何事情,绝对不会对应该加载的 SWF 做任何事情再次加载以应用它们(变量)。

因此,举这个简单的例子:

HTML :

<div id='swf_container'>
    <embed id='swf_object' src='swf.swf' flashvars='id=1' />
</div>

JavaScript :

// change the flashvars attribute
var swf_object = document.getElementById('swf_object');
    swf_object.setAttribute('flashvars', 'id=2');

var swf_container = document.getElementById('swf_container');

var inner_html = swf_container.innerHTML;

// reload the swf object
swf_container.innerHTML = '';
swf_container.innerHTML = inner_html;

这种方式当然有效,但每次我们需要它做某事时重新加载 SWF 对象可能不是一个好主意,这就是为什么我们有 ExternalInterface 在 SWF 和 JavaScript.


因此,如果您可以访问 ActionScript 代码来创建该 SWF,则可以使用 ExternalInterface 在 SWF 已经加载时调用它中的任何函数。

为此,举个例子:

动作脚本:

if(ExternalInterface.available)
{
    // registers an AS function to be called from JS
    ExternalInterface.addCallback('from_JS_to_AS', from_JS);
}

function from_JS(id:int) : void 
{
    // use the id sent by JS
}

JavaScript :

var swf_object = document.getElementById('swf_object');
    swf_object.from_JS_to_AS(1234);

...并且不要忘记使用 swfobject 来避免某些浏览器兼容性并确保您在 ActionScript 端和 JavaScript 端之间建立通信 ...

希望能帮到你。