简体   繁体   English

IE中的Flash和JavaScript通信

[英]Flash and JavaScript communication within IE

I am having in issue with IE passing a string back into an swf using the EternalInterface class in Flash CS4. IE使用Flash CS4中的EternalInterface类将字符串传递回swf时遇到问题。

I have an swf with the following code: 我有以下代码的瑞士法郎:

var externalString:String = ExternalInterface.call("IncomingJS")

which is inside an event listener attached to an Event.ENTERFRAME and an if statement waiting for ExternalInterface.available. 在附加到Event.ENTERFRAME的事件侦听器和等待ExternalInterface.available的if语句中。

The IncomingJS function looks like: IncomingJS函数如下所示:

function IncomingJS() {
   return stringFromHTML;
}

and sits on the HTML page with the swf. 并使用swf坐在HTML页面上。

I am able to successfully get the externalString variable and procceed with the rest of the AS3 script in Firefox, Safari and Chrome, but not in IE. 我能够成功获取externalString变量,并在Firefox,Safari和Chrome中使用其余的AS3脚本进行处理,但在IE中则不能。

If I add in an alert (stringFromHTML) before the return statement in the Javascript, I get the value of the stringFromHTML spammed, which looks like Flash is firing the function at the right rate. 如果我在Javascript的return语句之前添加alert (stringFromHTML)stringFromHTML垃圾邮件stringFromHTML的值,这似乎是Flash以适当的速率触发了该函数。

The embed code in HTML for the swf is a little simple: swf的HTML嵌入代码有点简单:

<object width="750" height="200" id="controlledScale"><param name="movie" value="http://www.myURL.com/controlledScale.swf"><param name="allowScriptAccess" value="sameDomain" /><embed src="http://www.myURL.com/controlledScale.swf" width="750" height="200" allowScriptAccess="sameDomain"></embed></object>

Any help or advice would be greatly appreciated. 任何帮助或建议,将不胜感激。

Thanks, DavidB 谢谢,DavidB

Edit 编辑

I realise how poor the SWF embed code is. 我意识到SWF嵌入代码有多糟糕。 Unfortunately, the HTML code is actually working within a 3rd party HTML generator, and one of it's limitations is that I can only have a single line (with unlimited length) of html at a time. 不幸的是,HTML代码实际上是在第三方HTML生成器中运行的,其局限之一是我一次只能有一行(无限长)的html。

Are the other options (swfObject etc) able to run either with no line breaks in the code, or would I be asking for trouble with Javascript and the SWF to, instead of embedding the SWF directly, use something like an iFrame and refer to a 'proper' flash delpoyment html file? 其他选项(swfObject等)是否能够在代码中没有换行符的情况下运行,或者我是否会要求Javascript和SWF麻烦,而不是直接嵌入SWF,而是使用类似iFrame的东西并引用“正确的” Flash delpoyment html文件?

Kind of at a point on this one where I'm not even sure where the problem is actually located. 在这一点上,我什至不确定问题的实际位置。 The swf's are find sending out to Javascript across all browsers, just not getting info back in IE only. 可以在所有浏览器中将swf发送到Javascript,而不仅仅是在IE中获取信息。

您必须在对象标记中添加一个id才能在IE中工作。

<object width="750" id="myflash" height="200"><param name="movie" value="http://www.myURL.com/controlledScale.swf"><param name="allowScriptAccess" value="sameDomain" /><embed src="http://www.myURL.com/controlledScale.swf" width="750" height="200" allowScriptAccess="sameDomain"></embed></object>

I pretty sure there is a security "feature" in IE that stops JS being called too many times from Flash, to stop it crashing the browser. 我很确定IE中有一个安全性“功能”,可以阻止从Flash调用JS太多次,以防止它崩溃。

Is there a reason why you have to call it every frame? 有理由为什么必须每帧都调用它吗?

I'd suggest against this at all costs as it's putting a LOT of extra stress on the browser. 我建议不惜一切代价,因为它给浏览器带来了很多额外的压力。

***** EDIT ***** *****编辑*****

If you want to call an ExternalInterface method from JS -> Flash in IE you have to reference the object slightly differently, like this: 如果要从IE中的JS-> Flash调用ExternalInterface方法,则必须稍微不同地引用该对象,如下所示:

function thisMovie(movieName) {
  if (navigator.appName.indexOf("Microsoft") != -1) {
    return window[movieName];
  } else {
    return document[movieName];
  }
}

Then once you're sure the string you want to pass is constructed correctly you can call it like this from the JS: 然后,一旦确定要传递的字符串正确构建,就可以从JS这样调用它:

thisMovie( "theFlashElementID" ).giveMeMyStringAlready();

Then in your Flash you would have something like this: 然后在您的Flash中,您将得到以下内容:

if( ExternalInterface.available ) 
{
  ExternalInterface.addCallback( "giveMeMyStringAlready", handleTheStringFromJS );
}
  else
{
  handleTheFactIDontHaveExternalInterfaceAvailable();
  // the only reason this would be is if container that 
  // is embedding the swf isn't fully loaded by the browser
}

The standout line from the AS3 docs regarding ExternalInterface is this: AS3文档中关于ExternalInterface的突出特点是:

Note: When using the External API with HTML, always check that the HTML has finished loading before you attempt to call any JavaScript methods. 注意:将外部API与HTML一起使用时,在尝试调用任何JavaScript方法之前,请始终检查HTML是否已完成加载。

This: 这个:

Unfortunately, the HTML code is actually working within a 3rd party HTML generator 不幸的是,HTML代码实际上在第三方HTML生成器中运行

is the problem. 是问题。

The swf is sitting inside a <form> tag. SWF位于<form>标记内。 At the browser stage, there is a huge volume of really verbose code, and I missed the tags at the very beginning and end of the html code. 在浏览器阶段,有大量真正冗长的代码,我错过了html代码开头和结尾的标签。

Thanks for the help. 谢谢您的帮助。 If I have learnt nothing else from the experience, it's to be full with the question and look well beyond the immediate problem, breaking each element down as fully as I can. 如果我没有从经验中学到其他东西,那就是要充裕的问题,并且要超越眼前的问题,并尽可能地分解每个要素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM