最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Execute js after swf load - Captivate - Stack Overflow

programmeradmin1浏览0评论

I'm trying to execute some Javascript once the swf file has finished loading.

While I did find a great post on this, How to check if a swf is loaded using JavaScript with swfobject?

But I'm unsure as to how to use pipwerks advise, as Adobe Captivate embeds there swfs a bit differently, see code below.

    //Embed SWf using the SWFObject library
    var so = new SWFObject(CONFIG.FILENAME, CONFIG.SWFOBJ_ID, 
                                    CONFIG.COURSE_WIDTH, CONFIG.COURSE_HEIGHT, 
                                    CONFIG.FPVERSION, "#CCCCCC");

        so.addParam("quality", "high");
        so.addParam("wmode", CONFIG.WMODE);
        so.addParam("bgcolor", CONFIG.BGCOLOR);
        so.addParam("menu", "false");
        so.setAttribute("name", CONFIG.SWFOBJ_NAME);
        so.setAttribute("redirectUrl", CONFIG.REDIRECT_URL);
        so.addParam("AllowScriptAccess","always");
        so.write(CONFIG.TARGET);

Where would I place the callback function?

I'm trying to execute some Javascript once the swf file has finished loading.

While I did find a great post on this, How to check if a swf is loaded using JavaScript with swfobject?

But I'm unsure as to how to use pipwerks advise, as Adobe Captivate embeds there swfs a bit differently, see code below.

    //Embed SWf using the SWFObject library
    var so = new SWFObject(CONFIG.FILENAME, CONFIG.SWFOBJ_ID, 
                                    CONFIG.COURSE_WIDTH, CONFIG.COURSE_HEIGHT, 
                                    CONFIG.FPVERSION, "#CCCCCC");

        so.addParam("quality", "high");
        so.addParam("wmode", CONFIG.WMODE);
        so.addParam("bgcolor", CONFIG.BGCOLOR);
        so.addParam("menu", "false");
        so.setAttribute("name", CONFIG.SWFOBJ_NAME);
        so.setAttribute("redirectUrl", CONFIG.REDIRECT_URL);
        so.addParam("AllowScriptAccess","always");
        so.write(CONFIG.TARGET);

Where would I place the callback function?

Share Improve this question edited May 23, 2017 at 10:25 CommunityBot 11 silver badge asked Jan 23, 2014 at 8:26 MintMint 16k32 gold badges83 silver badges109 bronze badges 3
  • are you using action script ? – Vedant Terkar Commented Jan 23, 2014 at 8:29
  • What version of SWFObject does it use? – net.uk.sweet Commented Jan 29, 2014 at 1:57
  • @net.uk.sweet SWFObject v1.5 – Mint Commented Feb 11, 2014 at 0:49
Add a ment  | 

5 Answers 5

Reset to default 4

You're using an older version of SWFObject (1.?) which doesn't support the callback functionality which is available in version 2. You can see the parameters supported by the version you're using here.

If you update SWFObject to the latest version (2.2) you can pass your callback function in using the embedSWF function:

swfobject.embedSWF("movie.swf", "flashcontent", "550", "400", "9", false, false, false, false, callback); 

All the other properties you're currently passing to SWFObject are supported in the latest version so, assuming you've got control over the HTML markup and JavaScript (which it sounds like you do), upgrading to the newest version of the library is probably your best bet.

If you are using the latest version, you can put the callback in just as net.uk.sweet described:

swfobject.embedSWF("movie.swf", "flashcontent", "550", "400", "9", false, false, false, false, callback);

Instead of worrying about an ActionScript callback, you should be able to detect when the SWF file itself is finished loading.

To do this, you'll first have to figure out how you can snag the object or embed element. The easiest method would be to add a line so you can add an ID to the element.

To the code block you posted above, before so.write, add:

so.addAttribute('id', 'myswf');

This should add id="myswf" to the actual generated HTML.

Then, you want to add an event listener which grabs this value and adds a load event. This should go after the so.write line.

var obj = document.getElementById('myswf');
obj.addEventListener('load', /* callback goes here */);

There is also one potential edge case you have to contend with. If the SWF is already loaded, it might not trigger the event listener. There are two ways to contend with this.

1) After CONFIG.FILENAME, add a random string. This will force the video to always reload. This isn't a great solution because it forces the video to always reload, but if the SWF is small, it works. It would look something like this:

var so = new SWFObject(CONFIG.FILENAME + '?' + (new Date()).getTime(), CONFIG.SWFOBJ_ID, 
                                    CONFIG.COURSE_WIDTH, CONFIG.COURSE_HEIGHT, 
                                    CONFIG.FPVERSION, "#CCCCCC");

(Using (new Date()).getTime() to generate a unique timestamp for our random string.)

2) Update your SWFObject to the latest version. Unfortunately, for objects there aren't any other workarounds I know for how to deal with this. Depending on your exact circumstances you could do some other tricks, but we'd need more info about what you want to do with it.

I would rather use old school solution – from inside of the flash set onLoad to call javascript function. And if you are loading movies dynamically you can create preroader.swf and pass url of movie as variable. This is more stable solution

There is a property PercentLoaded for SWFObject objects.

Look at this tutorial to help for implementation : link

Well you are in Luck! swfjsPreLoader The second paragraph seems to serve your problem.

发布评论

评论列表(0)

  1. 暂无评论