Javascript – SwfAddress conflict with SwfObject’s ‘callback’ argument

callbackconflictjavascriptswfaddressswfobject

For a current project we are using SwfObject 2.2 to embed flash files, and the CRD gurus are using SwfAddress 2.3 to create SEO flash goodness.

Turns out that if you have both libraries included on a page, then any attempts at using SwfObject callback in the API (http://code.google.com/p/swfobject/wiki/api) prevents the SwfObject loading. In the example you can toggle this simply by HTML commenting out the SwfAddress file.

Sorry I couldn't point to absolute URLs for these two libraries in my code below.

<head>
    <title>SWFObject 2.2 dynamic embed with callback</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <script type="text/javascript" src="swfobject.js"></script>
    <script type="text/javascript" src="swfaddress.js"></script>
    <script type="text/javascript">
    function outputStatus(e) {
        alert("e.success = " + e.success +"\ne.id = "+ e.id +"\ne.ref = "+ e.ref);
    }
    var params = {};
    params.allowscriptaccess = "always";

    swfobject.embedSWF("http://www.bobbyvandersluis.com/swfobject/testsuite_2_2/test6.swf", "myContent1", "300", "120", "9.0.0", "expressInstall.swf", null, null);
    swfobject.embedSWF("http://www.bobbyvandersluis.com/swfobject/testsuite_2_2/test6.swf", "myContent2", "300", "120", "9.0.0", "expressInstall.swf", null, params, null, outputStatus);
    </script>
</head>

<body>
    <div id="myContent1">
        <h1>Alternative content</h1>
        <p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>
    </div>
    <div id="myContent2">
        <h1>Alternative content</h1>
        <p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>
    </div>
</body>

Any ideas? Thanks in advance!

Best Answer

This was a tough one.

Quick answer: You need to pass empty objects instead of "null" for the flashVars and attributes: (See my corrected demo code here)

swfobject.embedSWF("http://www.bobbyvandersluis.com/swfobject/testsuite_2_2/test6.swf", "myContent1", "300", "120", "9.0.0", "expressInstall.swf", {}, {});
swfobject.embedSWF("http://www.bobbyvandersluis.com/swfobject/testsuite_2_2/test6.swf", "myContent2", "300", "120", "9.0.0", "expressInstall.swf", {}, params, {}, outputStatus);

Full answer: Digging into the SWFAddress source code, they are rewriting the SWFObject embed functionality so they can inject their own. One of the things they have to do to do this is transfer all of the parameters you pass in to their own function. The "null" you were passing for the attributes object was causing an error in the SWFAddress code here:

var _s2e = swfobject.embedSWF;
    swfobject.embedSWF = function() {
    _args = arguments;
    if (typeof _args[8] == UNDEFINED)
        _args[8] = {};
    if (typeof _args[8].id == UNDEFINED)
        _args[8].id = _args[1];  // <-- ERROR here when this parameter (attributes) is null.
        _s2e.apply(this, _args);
        _ref.addId(_args[8].id);
    }

The error caused the whole second embed to fail.