开发者

Javascript for dynamically loading the SWF

I need a Javascript or Ajax to load SWF files in a folder.

/swfdir
 |_ test_1.swf
 |_ test_2.swf
 |_ test_3.swf
 .
 .
 .
 |_ test_50.swf

A folder has number of swf files. I want load all of them one b开发者_运维知识库y one, by replacing existing one. each flash should play for 10 secs, then it must be replaced with next swf file.

Please provide me a working snippet.

UPDATE:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
        <title>SWFObject 2 static publishing example page</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">
            function loadSWFObjects() {
                var i=1;
                for(i=1;i<=50;i++) {
                    document.getElementById("mySWF").innerHTML = 
                        "<object type=\"application/x-shockwave-flash\" data=\"SWFObjects/test_"i".swf\" width=\"300\" height=\"120\">";
                }
            }
        </script>
    </head>
    <body>
        <div>   
            <object id="mySWF" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="300" height="120">
                <object type="application/x-shockwave-flash" data="SWFObjects/test_1.swf" width="300" height="120">
            </object>
        </div>
    </body>
</html>

How to call that function after loading the page? This code is not working for me :(

Any help indeed.

thanks


At a minimum, your code isn't working because you have a syntax error here -- you're not concatenating your "i" value:

document.getElementById("mySWF").innerHTML = 
                        "<object type=\"application/x-shockwave-flash\" data=\"SWFObjects/test_"i".swf\" width=\"300\" height=\"120\">";

It should be like this:

.../test_" + i + ".swf...

Given your requirement, you'd be much better off using something like Flowplayer. If you just use javascript, all you can do is something like setInterval(), and javascript won't know anything about the state of the swf -- if something prevents the swf from playing within 10 seconds (or whatever you set the interval at), the script will load the next swf anyway -- very brittle design. Flowplayer uses playlists where you can define a list of any length, and the next video will only play once the previous is done -- regardless of how long it takes. Take a look at this sample. I use JSON to return dynamic lists of variable length. Use whatever server-side language is at your disposal to inspect your file system and return the list of file names.

If you simply cannot use Flowplayer, note that HTML object syntax varies according to browser/version. I see that you're loading swfobject.js, yet you're not using any swfobject functionality. Why? Take a look at the swfojbect javascript API.

That said, the following script will handle the interval functionality:

var loadSWFObjects = function() {
    var i = 1;
    var sInt = setInterval(function() {
        if(i >= 50) {
            clearInterval(sInt);
        }else{
            i++;
        }
        document.getElementById("mySWF").innerHTML = "<object type=\"application/x-shockwave-flash\" data=\"SWFObjects/test_" + i + ".swf\" width=\"300\" height=\"120\">";
    }, 10000);
}

And just do this if you want it to occur on page load:

window.onload = loadSWFObjects;

If you need a more flexible/modern onload handler, try this or google.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜