document.ready for embedSWF problem
| Project: | SWFAddress |
| Version: | 6.x-1.x-dev |
| Component: | Code |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | by design |
Jump to:
function theme_swfaddress_swfobject uses the document.ready function as a callback to embed the swf into the appropriate div tag ID. Unfortunately, this can cause issues where alternative content "flashes/displays" for a second before the flash loads.
Based on the swfobject documentation, the correct implementation of the dynamic swfobject approach is to just call the swfobject.embedSWF function (no need for the document.ready callback). I believe that the swfobject has built in handling for when it's appropriate to do the dynamic placement.
I modified my version of the module and tested this on multiple browsers and all seems to be working as desired now.
Here is how my function looks:
function theme_swfaddress_swfobject($swf_address, $flash_tag_id, $flash_width, $flash_height, $flash_version,
$flash_background, $flash_replace_id) {
$swfaddress_js = <<

#1
The complete function was cut off in my post. Here are the changes I made:
$(document).ready(function(){swfobject.embedSWF("%s%s", "%s", "%s", "%s", "%s", false, {}, {bgcolor: "#%s"}, {id: "%s", name: "%s"});
});
to:
swfobject.embedSWF("%s%s", "%s", "%s", "%s", "%s", false, {}, {bgcolor: "#%s"}, {id: "%s", name: "%s"});#2
Hi Justin,
Wouldn't this cause a problem if the alternative content div hasn't yet loaded at the time that swfobject.embedSWF is called? I think that's the reason behind the use of document.ready.
Stephen
#3
DigitalFrontiersMedia is correct, we included the jQuery document.ready call to ensure the SWF embed comes after the DOM has finished loading. SWFObject does not have a dependency on jQuery, which is why its documentation won't include a document.ready() call in the examples.
Also, this is a theme_ function, which means that if you aren't happy with the output, you can override it in your theme's template.php.
For these reasons, I am marking this 'by design' and we will not be changing the module to address this.
#4
I didn't know it was a theme_ function. That rocks. Good thinking.
#5
Changing this in the theme would be much more appropriate.
DigitalFrontiersMedia did bring up a good point that if the DIV is not loaded, then trying to replace the div with a javascript call would fail.
However, I'm a bit confused though. Why would the swfaddress module need to use the JQuery document.ready() call to check and see if the div exists? Doesn't the swfobject JS already have built in checking for that?
Looking at the JS and doing a bit of research, it definitely appears that SWFObject has built-in dom DomContentLoaded checking.
As an example, from line 61 after you format in eclipse.
j.addEventListener("DOMContentLoaded", f, false)In fact, much of the swfobject js code seems dedicated to testing whether or not an element is ready or not.
Isn't the document.ready() call just overriding the swfobject functionality then?
Bottom line is that flashing any alternative content to the user is not ideal.