PDA

View Full Version : Target Flash with Javascript



spazcer
February 18th, 2008, 02:26 PM
So I'm using the External API in flash to call a function in flash every time a javascript event occurs. The flash side is working fine but the javascript side works in every browser except certain version of IE. Here's the javascript code:


function getFlashMovieObject(movieName)
{
if (window.document[movieName])
{
return window.document[movieName];
}
if (navigator.appName.indexOf("Microsoft Internet")==-1)
{
if (document.embeds && document.embeds[movieName])
return document.embeds[movieName];
}
else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
{
return document.getElementById(movieName);
}
}


function changelNamenow() {
var fPlane=getFlashMovieObject("plane");
fPlane.changelName(document.getElementById("LastN").value);
}
function changefNamenow() {
var fPlane=getFlashMovieObject("plane");
fPlane.changefName(document.getElementById("FirstN").value);
}
function changecompanynow() {
//alert(document.getElementById("Company").value);
getFlashMovieObject("plane").changecompany(document.getElementById("Company").value);

}To see the final effect go to http://power.magicomm.biz and type your name into the First Name and Last Name fields. The javascript uses onBlur() to call a function in flash that loads the contents of the input box and displays them on the newspaper accordingly.

So this is really bugging me that i cant get this to work in IE so any help would be greatly apreciated.

icio
February 18th, 2008, 08:03 PM
I think the problem might be that you're using some old-school methods for referencing the elements.

You should be using getElementById when you're wanting to set a variable to reference an element. If you need to get an element with respect to that element (for example, you might want to get the embed element within an object element) you should use getElementsByTagName. The following is a quick example:


<object id="my_flash">
<param />
<param name="movie" value="" />
<embed src="" />
</object>

function browserRequiresEmbed() {
// return false if browser requires object element to reference flash movie
// return true if browser requires embed element to reference flash movie
}
function getFlashElement(id, embed) {
var e = document.getElementById(id);
if (!e) return false;
if (!embed) return e;
return e.getElementsByTagName("EMBED")[0];
}
var flash = getFlashElement("my_flash", browserRequiresEmbed());
if (!flash) alert("this example sucks or you used it wrong");

Hope that helps. You're best to take my example with a pinch of salt.
Edit2: Here was my thinking behind my ramblings: http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_4150&sliceId=1

Edit: Sorry, I misread your code. You are in fact using getElementById. I also see that you're trying to over-ride the browser specific one in-case it doesn't work very well. It's normally safe to accept that getElementById works just fine, assuming that HTML validates. Perphaps my suggestion will still work.