05-10-2007, 06:22 AM
|
#1
|
|
Senior Member
Join Date: Nov 2002
Location: Irvine, CA.
Posts: 256
|
How to get Flash's OBJECT/EMBED ID???
Is there a way for a SWF to know the ID(s) that was used to OBJECT/EMBED it into the HTML page?
|
|
|
05-10-2007, 07:37 AM
|
#2
|
|
Resu Deretsiger
Join Date: Jul 2005
Location: St-Petersburg, Russia
Posts: 2,330
|
Donno how you would do this, but out of curiosity - what is the scenario you are considering?
__________________
overstream.net: add subtitles to online videos (youtube, vimeo, blip.tv...).
|
|
|
05-10-2007, 05:08 PM
|
#3
|
|
Senior Member
Join Date: Nov 2002
Location: Irvine, CA.
Posts: 256
|
Detect Mouse Outside of Flash
Pure laziness. I'm trying to get the mouse loc outside of the Flash window, using a Flash-only solution. I do this using an undocumented side-effect of the way JavaScript and ExternalInterface.call() work together, namely that you can pack an entire function into the outbound message, and not just a javascript function name:
Code:
// Assumes you have a button called "testBtn" and a dynamic field called "testField"
import flash.external.ExternalInterface;
testBtn.onRelease = function() {
var flashObjectName:String = "MySwf";
var methodName:String = "ReportMouseLoc";
var instance:Object = null;
var method:Function = ExternalMouseLoc;
var wasSuccessful:Boolean = ExternalInterface.addCallback(methodName, instance, method);
function ExternalMouseLoc(MouseX, MouseY) {
testField.text = MouseX+":"+MouseY;
}
var nullCall = ExternalInterface.call("function(){document.onmousemove = function(){var e = arguments[0] || event;window['"+flashObjectName+"'].ReportMouseLoc(e.clientX,e.clientY )}}");
};
 It's my cool script of the day; that little overstuffed javascript tells IE to start force-feeding the mouseloc back to the ExternalMouseLoc function when the mouse is outside the swf. The amazing thing is that the function actually persists after the call() is made, even though there's no supporting code in the HTML document. It works great considering that everyone's told me it's impossible.
(I think AS3 has a better way of doing this, but this method works in AS8/Flash8, too.)
THE CATCH is I that I need to explicitly pass in the swf's Object ID like you see in the very first line so that the callback function can find it. So if the ID(s) of the movie isn't exactly "MySwf" as above, it won't be able to send the callback message.
Hence my question; if there was a way to get the ID as the browser sees it, I could universalize the script, and turn it into a proper AS2 class.
FOR THOSE TRYING THE SCRIPT AT HOME:
1) If you are testing, make sure you set allowScriptAccess in both the OBJECT and in the EMBED to "always" so it will run from your desktop; Flash's default settings of "samedomain" won't run locally. Beat my head against a wall for two hours over that one!
2) It will work in IE 6+, but probably not in any others, since the overtuffed JS function has been trimmed down to see if it's even possible. Once I get a demo going, I'll post a better version of it.
|
|
|
05-10-2007, 07:11 PM
|
#4
|
|
Senior Member
Join Date: Nov 2002
Location: Irvine, CA.
Posts: 256
|
Here's the code in action:
BrowserMouse
|
|
|
05-11-2007, 02:43 AM
|
#5
|
|
Resu Deretsiger
Join Date: Jul 2005
Location: St-Petersburg, Russia
Posts: 2,330
|
Niiiiice! Hats off, Arodicus!
OK, if you are in the business of injecting javascript into the surrounding page, you could do this: inject a js function that iterates through ALL the objects in the DOM - recursively (probably better do it on an interval), and on each of the objects it attempts to call a certain function (if it exists), e.g., "setObjectIdForFlash" or something of the sort, passing it the id of that object. Of course, your flash swf object will be the only one that actually has that function. The js "scanning interval" can be stopped once the flash object is found, but if you want to knock yourself out you should also account for the case when there are 2 or more "outside-id"-enabled swfs embedded into the page.
__________________
overstream.net: add subtitles to online videos (youtube, vimeo, blip.tv...).
|
|
|
05-11-2007, 03:00 AM
|
#6
|
|
Resu Deretsiger
Join Date: Jul 2005
Location: St-Petersburg, Russia
Posts: 2,330
|
E.g. something like this:
Code:
function walkTheDom() {
var items = document.getElementsByTagName("*");
var i=items.length;
var item;
do
{
item = items[i];
//Do something with item
}
while (--i);
}
(From http://geekswithblogs.net/mparsons/a.../02/71175.aspx ).
You are only interested in object/embed tags, of course, but must iterate over all the containers that can house them.
__________________
overstream.net: add subtitles to online videos (youtube, vimeo, blip.tv...).
|
|
|
05-11-2007, 05:24 PM
|
#7
|
|
Senior Member
Join Date: Nov 2002
Location: Irvine, CA.
Posts: 256
|
Doh! Never even thought of tackling this from the JS side... yeah, I could encapsulate that into a call() that really does return a value, fire that off first, and use the results to prime the callback function.
Great suggestion! Now the fun bit -getting it to work in all the browsers!
|
|
|
05-12-2007, 01:02 AM
|
#8
|
|
Resu Deretsiger
Join Date: Jul 2005
Location: St-Petersburg, Russia
Posts: 2,330
|
Well it's just basic DOM iteration, I think it should work in most browsers.
__________________
overstream.net: add subtitles to online videos (youtube, vimeo, blip.tv...).
|
|
|
06-19-2007, 08:40 PM
|
#9
|
|
Senior Member
Join Date: Nov 2002
Location: Irvine, CA.
Posts: 256
|
Quote:
Originally Posted by astgtciv
Niiiiice! Hats off, Arodicus!
OK, if you are in the business of injecting javascript into the surrounding page, you could do this: inject a js function that iterates through ALL the objects in the DOM - recursively (probably better do it on an interval), and on each of the objects it attempts to call a certain function (if it exists), e.g., "setObjectIdForFlash" or something of the sort, passing it the id of that object. Of course, your flash swf object will be the only one that actually has that function.
|
After thinking about this one a while, I think there's an even easier and less CPU-intensive way... simply crawl the DOM looking for an object that has a path parameter that's the same as your moviepath... time permitting I'll try to come up with a demo... Thanks for the tips!
|
|
|
06-20-2007, 03:00 PM
|
#10
|
|
Resu Deretsiger
Join Date: Jul 2005
Location: St-Petersburg, Russia
Posts: 2,330
|
Yep, that's true! To take care of the case in which the swf containing your code is loaded by another swf you could do something like
ActionScript Code:
var orig_lockroot:Boolean = _root._lockroot;
_root._lockroot = false; // make sure we have reference to the original _root
trace(_root._url); // now we can get the top _url
_root._lockroot = orig_lockroot; // reset the original _lockroot for this swf
Should be an <object> or an <embed> tag...
__________________
overstream.net: add subtitles to online videos (youtube, vimeo, blip.tv...).
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 03:18 AM.
|
|