Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 05-10-2007, 06:22 AM   #1
Arodicus
Senior Member
 
Join Date: Nov 2002
Location: CA
Posts: 454
Default 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?
Arodicus is offline   Reply With Quote
Old 05-10-2007, 07:37 AM   #2
astgtciv
Resu Deretsiger
 
astgtciv's Avatar
 
Join Date: Jul 2005
Location: St-Petersburg, Russia
Posts: 2,329
Default

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...).
astgtciv is offline   Reply With Quote
Old 05-10-2007, 05:08 PM   #3
Arodicus
Senior Member
 
Join Date: Nov 2002
Location: CA
Posts: 454
Wink 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.
Arodicus is offline   Reply With Quote
Old 05-10-2007, 07:11 PM   #4
Arodicus
Senior Member
 
Join Date: Nov 2002
Location: CA
Posts: 454
Default

Here's the code in action:

BrowserMouse
Arodicus is offline   Reply With Quote
Old 05-11-2007, 02:43 AM   #5
astgtciv
Resu Deretsiger
 
astgtciv's Avatar
 
Join Date: Jul 2005
Location: St-Petersburg, Russia
Posts: 2,329
Default

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...).
astgtciv is offline   Reply With Quote
Old 05-11-2007, 03:00 AM   #6
astgtciv
Resu Deretsiger
 
astgtciv's Avatar
 
Join Date: Jul 2005
Location: St-Petersburg, Russia
Posts: 2,329
Default

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...).
astgtciv is offline   Reply With Quote
Old 05-11-2007, 05:24 PM   #7
Arodicus
Senior Member
 
Join Date: Nov 2002
Location: CA
Posts: 454
Default

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!
Arodicus is offline   Reply With Quote
Old 05-12-2007, 01:02 AM   #8
astgtciv
Resu Deretsiger
 
astgtciv's Avatar
 
Join Date: Jul 2005
Location: St-Petersburg, Russia
Posts: 2,329
Default

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...).
astgtciv is offline   Reply With Quote
Old 06-19-2007, 08:40 PM   #9
Arodicus
Senior Member
 
Join Date: Nov 2002
Location: CA
Posts: 454
Default

Quote:
Originally Posted by astgtciv View Post
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!
Arodicus is offline   Reply With Quote
Old 06-20-2007, 03:00 PM   #10
astgtciv
Resu Deretsiger
 
astgtciv's Avatar
 
Join Date: Jul 2005
Location: St-Petersburg, Russia
Posts: 2,329
Default

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...).
astgtciv is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to play video from Flash's library Janek Mohavlec ActionScript 2.0 0 09-29-2006 04:20 PM
how to stop or clear flash's internal tween kensuguro ActionScript 2.0 5 02-06-2006 10:15 PM
How to put link in flash's generated HTML file?? ikevin Other Flash General Questions 1 03-14-2003 09:05 AM
How to put Flash's generated HTML into Frontpage ? ikevin Other Flash General Questions 1 03-14-2003 07:02 AM
What are good design reasons for having different levels in Flash's ActionScript? echezona Simple Stuff (Newbies) 8 05-01-2002 08:44 PM


All times are GMT. The time now is 03:55 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.