I'm wrote LABjs, so I can try to help field this question... 

So, it appears from your question that the issue of loading javascript files 
dynamically with LABjs is not really much related to your question of issues 
with swfobject. But let's try to sort this out.

1. You would use LABjs to load your "swfobject.js" file (along with any other 
scripts you may have on your page). This is safe as long as you are using v2.1 
or 2.2. Prior to 2.1, SWFObject used a document.write() and that was not safe 
for dynamic loaders like LABjs. However, LABjs has nothing to do with loading 
your SWF file itself -- that is done by SWFObject exclusively.

2. LABjs lets you run some code in a .wait() callback once the script is 
finished loading. So this is where you would put your 
swfobject.registerObject() calls. It might look like this:

$LAB
.script("swfobject.js")
.wait(function(){
   swfobject.registerObject(....);
   swfobject.registerObject(....);
   ....
});

3. However, the use of "registerObject" is not recommended in this case, and 
here's why: registerObject() *assumes* that it is being run *before* the page's 
dom-ready event has fired. It merely queues up a registration of what you want 
the flash to replace, and when dom-ready fires, it does the work. 

The problem with this is, dynamic script loaders like LABjs unpin script 
loading from dom-ready, meaning it's quite possible that your registerObject() 
calls could be called "too late" (that is, after dom-ready has already passed). 
In that case, the registerObject will never do anything except hide your object 
(the embed will not happen). However, the alternative, just using createSWF(), 
which assumes dom-ready is already passed, is also not safe, because it's 
possible that your .wait() *might* execute before dom-ready, in which case it 
would be firing too early.

Bottom line, this is a race condition you should avoid. So, you need to use 
safer logic, which happens to already exist inside embedSWF(), so something 
like this:

$LAB
.script("swfobject.js")
.wait(function(){
   swfobject.embedSWF(....);
   swfobject.embedSWF(....);
});

Note that embedSWF() is similar to registerObject() and createSWF(), but has 
slightly different parameters, so make sure you read the documentation 
carefully on what to pass to it.

4. Now, finally, your actual question, which is: "what happens if I call one of 
the swfobject functions, like embedSWF/createSWF/registerObject, and pass it an 
ID of an object that doesn't exist in my page?" The answer is, this will cause 
problems. But *that* has nothing to do with whether you used a loader like 
LABjs or not. SWFObject simply does not filter out any of its API calls against 
you passing in ID's that don't exist -- it assumes they are present since you 
called it. If you call it with a non-existent ID, you will likely see JS errors.

However, there's a way, using the API, that you can work around this problem:

$LAB
.script("swfobject.js")
.wait(function(){
   swfobject.addDomLoadEvent(function(){
      if (swfobject.getObjectById("myId")) {
         swfobject.embedSWF("my.swf","myId".....);
      }
      if (swfobject.getObjectById("myId2")) {
         swfobject.embedSWF("my2.swf","myId2".....);
      }
      ....
   });
});

We use addDomLoadEvent() to make sure that the getObjectById() calls happen 
after dom-ready. Since we're now doing a dom-ready check ourselves, technically 
we could switch to using createSWF() instead of embedSWF(), but I think it's 
better to stick with embedSWF() for your use case.

Hope all that helps clear things up. 

--Kyle






From: Aran Rhee 
Sent: Tuesday, August 17, 2010 10:36 PM
To: [email protected] 
Subject: Re: [SWFObject] Using LABjs with swfobject


yes, there is an issue with doing that :) 


swfobject tries to run its magic ondomready, and will be looking for the id's 
you specify. I


If your site already requires js, then you might want to look at the dynamic 
embed method. It allows a bunch more flexibility as to what to write to the 
page and when. You could make a wrapper function which you pass the correct dom 
id etc.


have a look at http://learnswfobject.com/ for a bunch of tuts which might cover 
exactly what you want to do.




Cheers,
Aran


On Tue, Aug 17, 2010 at 10:12 PM, BigBear <[email protected]> wrote:

  Hi All,

  Wondering if anyone has encountered any issues with the following
  setup:

  I'm using a parallel .js loader http://labjs.com/

  And I am putting the below into an external script:
     <script type="text/javascript">
     swfobject.registerObject("myId", "9.0.115", "expressInstall.swf");
     </script>

  Everything seems to be working great, however, throughout the site I
  wish to call a number of flash movies so it would be easiest to have a
  number of entries in my external .js, e.g.

     swfobject.registerObject("myId", "9.0.115", "expressInstall.swf");
     swfobject.registerObject("myId2", "9.0.115",
  "expressInstall.swf");
     swfobject.registerObject("myId3", "9.0.115",
  "expressInstall.swf");

  Is there any issue with adding these if the id is not actually found
  on the current web page though?

  e.g. I register myId3 for every web page but possibly this only shows
  up on some web pages within the site.

  I prefer the above method because it's more efficient and everything
  is contained in one file.

  Many thanks for any thoughts on this.

  --
  You received this message because you are subscribed to the Google Groups 
"SWFObject" group.
  To post to this group, send email to [email protected].
  To unsubscribe from this group, send email to 
[email protected].
  For more options, visit this group at 
http://groups.google.com/group/swfobject?hl=en.





-- 
You received this message because you are subscribed to the Google Groups 
"SWFObject" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/swfobject?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"SWFObject" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/swfobject?hl=en.

<<Emoticon1.gif>>

Reply via email to