views:

799

answers:

1

I have 2 javascripts in my head section. Now I want to place the embedSWF function inside the first script. Only I am not sure where..

        <!-- JQUERY Functions -->
    <script type="text/javascript"> 
        jQuery.noConflict();
        jQuery(document).ready(function() {
            // alert('DOM ready');
        });
        window.onload = function() {
            // alert('page completely loaded');
        }
    </script>

    <!-- write SWF -->
    <script type="text/javascript">
        function outputStatus(e) {
            if (!e.success) { // alert('NO flash, alternative content'); 
            } 
            else { // alert('flash embedded successfully'); 
            }
        }
        var flashvars = {};
        var params = {}
        swfobject.embedSWF("test.swf", "album-wrap", "930", "530", "9.0.0", "js/expressInstall.swf", flashvars, params, null, outputStatus); 
    </script>
A: 

You'd better move all the script from head to the bottom of the page, above the </body>. This will make the page load better, since loading javascript will block concurrent loading of other page element, such as css and image, that more required than the js code.

To put swfobject code, it's more safe to put it inside jQuery DOM ready block.

function outputStatus(e) {
  if (!e.success) {
    // alert('NO flash, alternative content'); 
  } 
  else {
    // alert('flash embedded successfully'); 
  }
}

jQuery(function($) {
  // alert('DOM ready');
  var flashvars = {};
  var params = {}
  swfobject.embedSWF("test.swf", "album-wrap", "930", "530", "9.0.0", "js/expressInstall.swf", flashvars, params, null, outputStatus);
});

This will make sure that the swfobject run after all required DOM have been loaded. If you do it outside the block, then it can have probability to run while still waiting the required DOM element to be loaded into page, that will give unexpected result.

Donny Kurnia
I tried inside document.ready and it works fine (for now)I am just worried because I saw Bobby van der Sluis reply here:http://groups.google.com/group/swfobject/browse_thread/thread/5ec050c79a7b8af6
FFish
Then you should not have any worries. What Bobby said is that SWFObject itself have mechanism to detect is DOM already loaded before running. It will not have any side effect if you put the code inside jQuery's DOM ready block. It's like using 2 safety mechanism, both will not affect the functionality of SWFObject.
Donny Kurnia
Bobby said " Your SWF and External Interface functionality will need some time to initialize, so calling it directly when the DOM is loaded is too quick. ". Well, calling it outside jQuery DOM loaded will make it loaded more quickly, because jQuery DOM ready block is there to wait until DOM element loaded. All code outside this block will be run even when DOM is not loaded full yet. I write my code like in my answers above, and I have no problems in all modern browser.
Donny Kurnia
Ok cheers Donny
FFish