A picture of me taking it easy

I'm currently on parental leave till September. During that time, I will not read any e-mail or blog comments.

Until I'm back, please read through my archives, take a look at my code/applications and check out my pictures.

Have a great summer, and a splendid winter to you aussies and kiwis! :-)

FlashReplace - A light-weight JavaScript to insert Flash movies into your web page

Published on Thursday, April 19th, 2007

Updated April 20th

I’m so used to having a $ method for accessing elements. Nevertheless, the idea is of course that FlashReplace should be stand-alone, so I replaced it with the standard document.getElementById.

Updated April 20th

Although I didn’t encounter any problems with not using an embed element (to have valid HTML code output), it has been strongly recommend that I use it to ensure maximum compatibility. Therefore, it has been added and the file size is now 2.1 KB.

Updated June 3rd

I’ve gotten a few e-mails asking how to add variables to the Flash movie, so here are some options:

The simplest way is to just do it through a querystring. I.e. “movieFileName.swf?testVar=hello”.

Another option is to reference the movie and do it something like this:

document.getElementById("movieName").SetVariable("testVar", "hello");

Ok, everyone and his mother are creating scripts to include Flash into a web page. I wanted to have my own version, but with less and more readable code, so here’s my JavaScript to include flash : FlashReplace.

Besides that I wanted to write the script myself, there were a few things about the two major scripts (that I’m sure you are all well aware of, SWFObject and UFO), that I didn’t like:

  • I didn’t find the code that easy to read, and tweak, if wanted.
  • The file size. SWFObject is about 6.7 KB and UFO is about 10.7 KB. I’m a sucker for small file sizes, so therefore FlashReplace is only 1.8 KB 2.1 KB. :-)
  • Neither of them created standards-compliant code; FlashReplace does. Not anymore. Now it creates an additional embed element for maximum compatibility
  • Several lines are needed to insert a Flash Movie. FlashReplace only needs one line.

I have the utmost respect for the people creating those libraries, and it is definitely likely that I’ve missed something when I wrote my script. Nevertheless, it seems to work fine in all major web browsers, so I share it with all of you in the hope that you will like it. And, of course, if you find any flaws, please let me know!

My version: FlashReplace

FlashReplace is a little object with a few parameters to include a Flash movie into a web page. It should also work fine with streamed movies and get us around the Eolas patent problem in IE. It basically just supports one parameter and one method:

defaultFlashVersion
A property to set the default Flash version to look for, if it isn’t set with the Flash movie creation.
replace
A method that replaces the content of a specified element within the web page with a Flash move. Basically, the element with the fallback content if Flash player isn’t available.

The parameters for the replace method

To be more specific, the replace method consists of a few parameters:

elmToReplace
The id of the HTML element where you want to replace the content with the Flash movie.
src
The path to the Flash movie (i.e. the swf file).
id
The id to give to the Flash movie element.
width
The width of the Flash movie.
height
The height of the Flash movie
version (optional)
The version of the Flash player needed to see the movie. Defaults to the defaultFlashVersion property of the FlashReplace object if not specified.
params (optional)
A way to add as many parameters as you want to the Flash movie.

Code examples

First, include the FlashReplace JavaScript file:

<script type="text/javascript" src="js/FlashReplace.js"></script>

Then, create a JavaScript block to insert the Flash movie/-s you want to:


<script type="text/javascript">
    FlashReplace.replace("flash-content", "/flash/my-movie.swf", "flash-element-id", 800, 200);
</script>

Naturally, you can put a JavaScript block exactly where in the code where you want each Flash movie to be created, you can have all of them in the same code block at the bottom of the page, or you can have them in an external file:


<script type="text/javascript">
    FlashReplace.replace("flash-content", "/flash/my-movie.swf", "flash-element-id", 800, 200);
    FlashReplace.replace("da-cool-header", "/flash/cool-header.swf", "flash-element-id", 600, 350, 7, {wmode : "transparent"});
    FlashReplace.replace("sidebar", "/flash/sidebar.swf", "flash-element-id", 200, 700, 7,
        {
            wmode : "transparent",
            quality: "high",
            bgcolor: "#ffffff"
        }
    );
</script>

What’s missing?

What it does miss, though, that the other JavaScript options contain, is a express install feature that I didn’t think was all that needed. I might add this in a future version if it’s really asked for, but at the time, simplicity and a very small file size were my main goals.

Feedback!

Please, give it a whirl, kick the tires. Tell me if it works fine for you, and your general take on it. Enjoy! :-)
Free Movies Free Movies

98 comments

  • Andy
    April 19th, 2007 at 17:26

    Can’t wait to try this one out!

  • inoodle
    April 19th, 2007 at 19:12

    Nice, I’m just trying it out at the moment.
    Not sure that it matters, but you’ve don’t have closing slashes on param elements…
    Its working though. I’ll update shortly.

  • inoodle
    April 19th, 2007 at 20:05

    Ok, all working very easily - perhaps I should test in something other than the fox though :).

    Anway, let me be the first to present…. wait for it… *drum roll*
    Unobtrusive Flash Replacement

    Or at least the first with your library :)

    Cheers,
    Aaron.

  • Harmen Janssen
    April 20th, 2007 at 0:12

    Great Robert, good job on keeping the file-size so low! :)

    @inoodle: closing slashes on HTML tags are only interesting if you feel the need to support XHTML.

  • RUDEWORKS » Archivo » Inclusión de Flash dinámica, válida y ligerísima.
    April 20th, 2007 at 0:54

    […] eres usuario SWFObject, te gustará FlashReplace. Archivado en: Flash javascript minipost programación […]

  • inoodle
    April 20th, 2007 at 1:39

    @Harmen
    Yeah I forgot about that HTML thing! :)

  • Blogg: JavaScript för Flash och kortkommandon | digital venues.
    April 20th, 2007 at 8:26

    […] kommer ett tips pÃ¥ tvÃ¥ användbara JavaScript. Robert Nyman har skrivit ett litet och nätt skript för att infoga Flash-filmer pÃ¥ en webbsida. Och Binny VA har skapat ett smidigt skript för att hantera kortkommandon. Kolla […]

  • Robert Nyman - author
    April 20th, 2007 at 9:10

    Andy,

    Good luck!

    inoodle,

    Nice, thanks! And yes, the unclosed param elements are becuase it is meant as HTML. However, if you want it inan XHTML solution, you can easily tweak the file to ensure it’s valid XHTML.

    Harmen,

    Thanks!

  • James Oppenheim
    April 20th, 2007 at 10:30

    Looks like a nice solution. I will have to give it a run. I have been using SWFObject for ages now. Maybe I should reconsider.

  • inoodle
    April 20th, 2007 at 11:02

    The nice thing about using the $ in the previous version, was the ability to pass in an element directly, instead of the id - which removed the requirement to actually have an id on the element to replace.

  • inoodle
    April 20th, 2007 at 12:56

    Robert, I updated my example with your newer mods, but also did some slight modifications to the script myself: Unobtrusive Flash Replacement

    What was the main reason (/browser) needing the embed element BTW ?

  • Robert Nyman - author
    April 20th, 2007 at 14:13

    James,

    I hope you will like it!

    inoodle,

    Absolutely, you can tweak it to use the $ method if you want, but just in case you don’t, I wanted the file to able to work stand-alone.

    From what I’ve heard, other web browsers than IE might need the embed element in certain combinations/scenarios, so I thought it best to add it just to make sure.

  • links for 2007-04-21 | pollas.dk [permanent meta] » Blog Archive
    April 21st, 2007 at 8:22

    […] FlashReplace - A light-weight JavaScript to insert Flash movies into your web page - Robert’s tal… Lean, light-weight flash insert (tags: webdev javascript flash robertnyman) […]

  • Tamás
    April 22nd, 2007 at 10:44

    Hi Robert. Why do we need the embed element?
    <object type=”application/x-shockwave-flash” data=”something.swf” width=”any” height=”any”>
    <param name=”movie” value=”something.swf” />
    <param name=”loop” value=”false” />
    <param name=”menu” value=”false” />
    <param name=”quality” value=”high” />
    <param name=”bgcolor” value=”any” />
    </object>

    This little code isn’t enough? (using HTML, XHTML or javascript)

  • James Oppenheim » Blog Archive » SEO friendly Flash programming
    April 22nd, 2007 at 15:17

    […] the SWFObject seems to be the popular choice at the moment. However, I recently read about Robert Nyman solution, which looks very promising and at a small 2.1KB, who could you go […]

  • Robert Nyman - author
    April 23rd, 2007 at 10:01

    Tamás,

    As mentioned in the post and in a comment above, I personally haven’t had any problems without the embed element, but people with vast experience of Flash and compatibility have recommended me to use it.

  • Adam Messinger
    April 24th, 2007 at 23:52

    I’m not very happy with the size of SWFObject either, so I’ll definitely be considering this for future projects.

    You’re probably already aware of this, but the SWFObject folks had to change the name of their script from “FlashObject” because of legal grumbling from Adobe. You might want to consider something similar, just as a CYA measure.

  • Robert Nyman - author
    April 25th, 2007 at 9:20

    Adam,

    Please try it, and let me know if it doesn’t work out for you.

    I know about that with SWFObject , but at the end of the day, we’ll see where it leads.

  • Flash interaction disabled in Internet Explorer - Robert’s talk
    April 26th, 2007 at 11:42

    […] alternative solution to this problem is my FlashReplace […]

  • Bleu-Rouge blog » Blog Archive » links for 2007-04-27
    April 27th, 2007 at 5:26

    […] FlashReplace - A light-weight JavaScript to insert Flash movies into your web page - Robert’s talk (tags: flash javascript embed scripts code webdesign webdevelopment HTML library resources web) […]

  • Niko Nyman
    April 27th, 2007 at 8:00

    How do you pass FlashVars to the loaded SWF? The “old way”, by URL encoding them and adding after filename.swf?

  • Clarence Eddy
    April 27th, 2007 at 8:40

    In IE, FlashReplace shows a broken image while the Flash object downloads. It works as expected in FireFox. Any workarounds?

  • Robert Nyman - author
    April 27th, 2007 at 10:16

    Niko,

    You should be able to send in FlashVars, as a param in the object array described in the code above. Like this:

    {
    flashvars : “…”,
    quality: “high”,
    bgcolor: “#ffffff”
    }

    Clarence,

    Hmm, I haven’t seen that problem. But in that case I guess it’s a general problem that the Flash movie is too big? If that’s the situation, maybe you might have an extra need to preload the Flash movie before the replacement is done alternatively have one small Flash movie and that then loads the other Flash movie.

  • Creattica - Flash » Blog Archive » The Weekly Best of the Flash Web
    May 1st, 2007 at 7:53

    […] Robert Nyman has released FlashReplace, a lightweight JavaScript embed similar to SWFObject and UFO […]

  • Renato Carvalho
    May 1st, 2007 at 18:23

    Wow! Big job. And a small file size :)
    I’ll try it now!!!

  • Robert Nyman - author
    May 2nd, 2007 at 8:56

    Renato,

    Enjoy!

  • Martin Odhelius
    May 2nd, 2007 at 17:30

    Hi Robert,

    Nice work! As you mention both ufo and swfobject have their weaknesses. I prefer ufo though cause that one works with content type application/xhtml+XML and you can put all code in the head of the page which I think is nice. I haven’t had any time to test your script yet but when I read your text I presume that your script only works if you put the script block somewhere inside the body, or am I wrong? If so, do you have any plans to make a version where it is possible to put the script in the head (which I think is some sort of “javascript best practices”). And the other question is of course if your script is working with XHTML+XML? ;)

    Keep up the good work..
    Best Regards
    Martin

  • Robert Nyman - author
    May 3rd, 2007 at 9:05

    Martin,

    You can put the code wherever you want; be it at each Flash instance, in a script block in the document or in external JavaScript file.

    If it works with application/xhtml+XML? It depends… :-)
    It uses innerHTML to add the content to the web page. Certain web browsers doesn’t support that with the application/xhtml+XML MIME type, but, for instance, Firefox supports it from version 1.5.

  • Martin Odhelius
    May 3rd, 2007 at 13:20

    Nice! I’ll try it out as soon I get some spare time and return back with more feedback :)

  • Lukeperry
    May 16th, 2007 at 15:39

    The variable assignent doesnt seem to work. At least not the way IM doing it :-)

    If i want to ad a variable by the name of “testVar” with the value “hello” how would i do that?

  • Robert Nyman - author
    May 16th, 2007 at 20:17

    Lukeperry,

    If i want to ad a variable by the name of “testVar” with the value “hello” how would i do that?

    The simplest way is to just do it through a querystring. I.e. “movieFileName.swf?testVar=hello”.

    Another option is to reference the movie and do it something like this:

    document.getElementById(”movieName”).SetVariable(”testVar”, “hello”);

  • shane plasebo
    June 3rd, 2007 at 9:55

    I use runfoo instead of the other javascript mentioned above. It is only 1.89 KB and easy to implement. Give it a try…

  • Robert Nyman - author
    June 3rd, 2007 at 20:41

    shane,

    Which runfoo would that be? And, what with runfoo do you find easier to implement compared to FlashReplace?

  • olly killick
    July 16th, 2007 at 16:41

    freakin’ awesome dude, well done on writing some kick ass code!!!

  • James
    September 13th, 2007 at 17:11

    Any news on the IE7 bug in this as posted by Clarence Eddy April 27th, 2007 at 8:40?

    When loading the flash you get a missing image placeholder in IE7 for a short time before the flash appears - very annoying.

  • James
    September 13th, 2007 at 18:00

    Follow up to the i.e7 problem … taking out the
    ((window.ActiveXObject)? ' id="' + id +
    sorts it out… but what does it break…?

  • Robert Nyman - author
    September 13th, 2007 at 20:23

    Olly,

    Thanks!

    James,

    Nope, sorry. Personally, I haven’t seen that problem, so my advice to Clarence still stands, to use a preloader in the Flash movie, if the size is the problem.

    The part with ActiveXObject is there since IE seemed to need a certain classid to properly render, and the src for setting the path to the Flash movie. If you remove that check and serve it to all web browsers, it might not work in Firefox and others. If you remove that code completely and it works in all web browsers, well, go for it then. :-)

  • F-LOG-GE » Blog Archive » SWFObject war tot, es lebe SWFObject
    October 1st, 2007 at 22:54

    […] mal wisssen, wofür die 11 KB sinnvoll sind. Ich habe bisher auch recht gerne Robert Nymans “FlashReplace” genutzt, das gerade mal um die 2 KB […]

  • Rich
    October 12th, 2007 at 17:10

    Could you post a full example of how to use it?

  • Robert Nyman - author
    October 16th, 2007 at 11:34

    Rich,

    The code example in the post is exactly how you would use it when you were to insert a Flash movie within your document.

  • Ansgar Hein
    October 19th, 2007 at 18:07

    Great script and also pretty lightweight. However it took me hours and some good advice from others who had similar problems to figure out how to understand the manual.

    1. Put the for FlasReplace.js in the section.
    2. Create your HTML-element that you would like to replace, for example use and put some alternative content inside.
    3. Put the other for replacing your directly behind it in your HTML-code.

    That does the whole trick and everything else is - as said above - brilliant.

    Thank you, Robert.

  • Rich
    October 22nd, 2007 at 12:38

    I did exactly what you said but it only worked when i put this part:

    FlashReplace.replace(”flash-content”, “/flash/my-movie.swf”, “flash-element-id”, 800, 200);

    In a function called MovieLoad as below:

    window.onload=MovieLoad;

    Otherwise, the script wasn’t being triggered. Is there a better way of doing this?

  • Robert Nyman - author
    October 22nd, 2007 at 13:21

    Rich,

    Make sure that the script is placed after the HTML element. My guess why it doesn’t work for you is that you try and reference the HTML element before it actually exists.

    Do it like this:


    <div id="flash-content">Where the Flash movie will be shown</div>

    <script type="text/javascript">
    FlashReplace.replace("flash-content", "/flash/my-movie.swf", "flash-element-id", 800, 200);
    </script>

  • Rich
    October 22nd, 2007 at 13:41

    GREAT!

    Yes, you were right. I’ve done as you said above and it works fine now, thanks!

    Great job, it is one of the simpler methods for including Flash around and as you say, more efficient, i’ll be using it from now on for all my Flash projects! Cheers!

  • Robert Nyman - author
    October 22nd, 2007 at 15:52

    Rich,

    Good to hear! Good luck! :-)

  • Jarrad
    November 14th, 2007 at 11:13

    new favourite, thanks!

  • Robert Cazan
    November 29th, 2007 at 21:19

    hello Robert.

    great method. It works great. But i have one little question…If i have multiple flash movies in a site, the script doesn’t work. it shows only the last movie.

    what changes do i have to make to the script in order to become efficient and work for 2 flash movies?

    thank you

  • Robert Nyman - author
    November 30th, 2007 at 8:38

    Robert Cazan,

    Thank you! Do you have any example where I can look at? Because it should work with as many Flash movies as you want (given that you insert them into different elements, of course), just like one of the examples shown in this article.

  • Robert Cazan
    November 30th, 2007 at 20:57

    yes. the address is:
    http://www.robertcazan.ro/test/testing.html

    but i figured it out…finaly :)
    i used

    div id=”flash-content2
    script type=”text/javascript”
    FlashReplace.replace(”flash-content” …

    i think you can see the mistake… :( but it’s ok now because i found out :D
    i was changing only at div id, and not in the “FlashReplace.replace(”flash-content“” too…

    thanks anyway for your reply…i appreciate it.

  • Robert Nyman - author
    December 2nd, 2007 at 23:20

    Robert Cazan,

    Greeat to hear! Good luck with things now! :-)

  • Robert Cazan
    December 24th, 2007 at 1:21

    hello again.
    I’m still working on my website, though Christmas is coming :D i have a question for you. Why Firefox doesn’t show the flash (by using your method)? It works just fine in IE but in Firefox it doesn’t… :(
    can you help me?

    www.robertcazan.ro
    Thanks,
    Robert

    ps: Merry Christmas and a Happy New Year :)

  • Robert Nyman - author
    December 25th, 2007 at 20:40

    Robert Cazan,

    Hi. :-)
    I’m sitting on a Mac here, but tested your testing page (http://www.robertcazan.ro/test/testing.html) and it works fine in both Firefox and Safari.

    I looked through your other pages, but couldn’t see you using FlashReplace anywhere else.

    Merry Christmas and Happy New Year to you too! I won’t read the comments for some time now, but please post a URL to a page where it doesn’t work, and I’ll take a look when I get back.

  • A Third (Smaller) Option for Embedding Flash Using JavaScript - Sitening Blog - Nashville SEO (Search Engine Optimization) and Web Design
    December 27th, 2007 at 16:23

    […] Now there’s a third viable option for embedding Flash using JavaScript. It’s called FlashReplace and was written by Robert […]

  • links for 2007-12-28 « toonz
    December 29th, 2007 at 0:24

    […] FlashReplace - A light-weight JavaScript to insert Flash movies into your web page - Robert’s talk (tags: flash javascript embed code webdev) […]

  • bakkelun
    December 29th, 2007 at 5:30

    Does this script add Wmode=Transparent as default? It’s very needed in my work environment, that’s why I ask.

    sincerely,
    -bakkelun

  • Robert Nyman - author
    December 29th, 2007 at 18:34

    bakkelun,

    No, not per default, since it’s fairly resource intensive. Just like the example in the post exemplifies, you need to specify this when you include the Flash movie. Like this:

    FlashReplace.replace("the-header", "/flash/cool-header.swf", "flash-element-id", 600, 350, 7, {wmode : "transparent"});

  • Marc
    January 5th, 2008 at 12:08

    Thanks a lot, the script works fine and is very useful! I had it running in less then 2 minutes.

  • Robert Nyman - author
    January 7th, 2008 at 8:48

    Marc,

    Great!

  • JamesM
    January 12th, 2008 at 13:34

    I seem to get a error in IE 6 and 7 “Operation Aborted error ” this is due to IE still loading and we are trying to write to the DOM area

    Now swfobject has fixed this error see below would be nice to try and fix this in Flashreplace.

    Operation Aborted error

    We added a try-catch block around the document.write statement (that inserts a deferred script to simulate a DOMContentLoaded event for Internet Explorer) to ensure it properly degrades to the onload event and to avoid an Operation Aborted error (which indicates that you try to access Internet Explorer’s DOM, while it is locked and it is not ready for DOM manipulations yet)

    if (ua.ie && ua.win) {
    try {
    document.write(”");
    var s = document.getElementById(”__ie_ondomload”);
    if (s) {
    s.onreadystatechange = function() {
    if (this.readyState == “complete”) {
    this.parentNode.removeChild(this);

  • Robert Nyman - author
    January 14th, 2008 at 8:37

    JamesM,

    Yes, I’m aware of this general problem in IE, which basically targets any script manipulating the DOM before it’s entirely loaded, not just FlashReplace.

    It is never a good approach to alter the DOM before it has loaded, so I’d rather recommend using code/a library which deals with changing content after the DOM has loaded (as opposed to when window.onload has been triggered). Options for that are ELO or the Load module in DOMAssistant.

    I wouldn’t want to include this functionality into FlashReplace itself, because I believe anyone scripting should use some script detecting when the DOM has loaded anyway, and then that funtcionality would just be duplicated in FlashReplace, resulting in a bigger file size with just superfluous code.

  • JamesM
    January 14th, 2008 at 22:40

    Hi Robert thank you for your detailed answer, I guess the main idea behind FlashReplace was to make it lightweight and not to hold extra superfluous or doubled up code. I am not a programmer and some times even have problems just tracking and understanding the code. So I will have to google and learn up how to try and call your code and read up on the links you posted thank you for your time and help.

  • Robert Nyman - author
    January 14th, 2008 at 22:43

    JamesM,

    No problem, I hope my answer helps you out.

  • Tim
    February 4th, 2008 at 21:18

    I am trying this out and it seems to work great. One issue I am having though is when using fullscreen video mode. It works fine if you have the newest Flash player installed, but I also need it to work with all players 9.0.28 or later. I know you can specify version number, but can I specify 9.0.28 instead of just 9? If so, how would I go about that?

  • Robert Nyman - author
    February 5th, 2008 at 9:59

    Tim,

    Hmm. I haven’t tested, but I think you could send in your value as a decimal. I.e., instead of this:

    FlashReplace.replace("da-cool-header", "/flash/cool-header.swf", "flash-element-id", 600, 350, 7);

    you should be able do to it like this:

    FlashReplace.replace("da-cool-header", "/flash/cool-header.swf", "flash-element-id", 600, 350, 9.0.28);

    or

    FlashReplace.replace("da-cool-header", "/flash/cool-header.swf", "flash-element-id", 600, 350, "9.0.28");

  • Tim
    February 5th, 2008 at 22:13

    Thanks. It worked when I passed the value with a comma and quotes. (IE- “9,0,28,0″) However, turns out that wasn’t my problem. Fullscreen mode only works if you have wmode set to “window”. I had “transparent”.

    Apparently the newest version of the Flash player (9.0.115) supports fullscreen and transparent, but the older versions (9.0.28+) do not. Hence, why I got it too work in my browser, but not in some others.

  • Robert Nyman - author
    February 6th, 2008 at 10:09

    Tim,

    Ah, intereresting. Good to hear that it worked out!

  • Peter Muusers-Meeuwsen
    February 7th, 2008 at 16:10

    Does anyone know if the flash file that is being loaded will be available through the browsers cache after a first inital load?

    Or does this method prevent caching because of the way the flash file is loaded?

    Anyone has any clue on this? ;)
    Best regards,
    Peter

  • Robert Nyman - author
    February 7th, 2008 at 17:10

    Peter,

    Good question. My estimate is that it will be cached, even though it’s put in the page via script, but I don’t have any facts to support that claim.

  • James
    February 17th, 2008 at 9:07

    I am trying to use FlashReplace rather than the bloated copy of
    SWFObject now the below works just fine in FF fox but does not seem to work well in IE

    You Can see the code that I have been using here

    http://pastebin.com/f35af2725

    The SWF file is displayed correctly but I do not think I am passing the correct parameters that the file needs for this flash application.

    The 3 other examples are how you would use SWFObject would it be possible if you could show me how to change the examples that are shown in SWFObject to be used in Flashreplace ? or to give me a few hints as my programming skills are very poor

  • Robert Nyman - author
    February 18th, 2008 at 9:52

    James,

    I took a quick look at your examples, and as far as I can see, you add the parameters correctly (just as you would with the other examples as well).

    Not sure what mssHolder refers to in the UFO examples, otherwise, you’d add parameters the same way with those examples.

  • James
    February 18th, 2008 at 12:13

    Thanks Robert I changed the path to be relative and it seems to have done the trick I will see if I can get the Dev of SWF file to move to your script as it seems to be much faster than SWFObject thank you for your hint.

  • Robert Nyman - author
    February 18th, 2008 at 12:46

    James,

    I’m glad to hear that it works!
    Good luck!

  • M.A.
    March 1st, 2008 at 19:39

    Great piece of code, I like it!
    You could insert an optional parameter with the HTML code to be shown if flash isn’t installed or if is it’s blocked on the client…

  • Robert Nyman - author
    March 3rd, 2008 at 8:57

    M.A.,

    Thank you! However, you should always have the HTML fallback (if JavaScript and/or Flash isn’t available) in the HTML code to begin with, and then use the script to replace it.

  • rev
    March 7th, 2008 at 18:39

    Ok guys, sorry but this code is not good. It doesn’t work properly in IE 7. In IE7 it shows a blank broken link box before it loads the next SWF AND all the code is way over complicated. Delete that FlashReplace.js and just use this code and it will work in all browsers no issue.

    Never forget, KISS! Keep it simple.

    if (document.embeds[’flashobject’])
    {
    document.embeds[’flashobject’].LoadMovie(0, ‘whatever.swf’);
    }
    else
    {
    document.getElementById(’flashobject’).movie
    = ‘whatever.swf’;
    }

  • Robert Nyman - author
    March 7th, 2008 at 19:03

    rev,

    If you encounter a black box, it seems like you’re not applying it correctly. Also, I’d prefer some constructive criticism instead of you just pimping your own approach.

    Your solution doesn’t support a fallback for non-JavaScript support, non-Flash support or accessibility or search engines. Quite important in my book.

    With that said, of course you should use whatever code you seem fit your needs best. That’s why there are a number of JavaScript libraries out there, where no solution is never the ultimate one for all thinkable scenarios.

  • Andreas
    April 9th, 2008 at 14:45

    As always great work. Know I’m kind of late but hadn’t seen this before and just reduced JS-size with 20k after replacing SWFObject with FlashReplace. Sweet.

  • Robert Nyman - author
    April 9th, 2008 at 18:47

    Andreas,

    Thanks! Good to hear that it worked well for you!

  • How to write valid HTML/XHTML code to include Flash - Robert’s talk - Web development and Internet trends
    May 27th, 2008 at 16:30

    […] An alternative suggested solution is using FlashReplace. […]

  • Ryan
    May 28th, 2008 at 12:55

    any ideas on how to trigger this with onClick?

    thanks

  • Robert Nyman - author
    May 28th, 2008 at 12:59

    Ryan,

    You could put the code in a function, e.g:

    function insertFlash () {
    FlashReplace.replace("flash-content", "/flash/my-movie.swf", "flash-element-id", 800, 200);
    }

    and then just call the insertFlash function when the onclick event occurs.

  • Ryan
    May 28th, 2008 at 15:14

    thx for the fast response!

    when i onClick it tries to execute the function but then just loads whatever video i have there in the first place.

    the first video is loaded thru HTML as your example above.

    other videos are loaded in its place by clicking the thumbnail.

    but when i click the thumbnail, the original just loads again. do i need to change the objectid?

    here’s my function:


    function loadVideo(src,width,height,divid,movieid)
    {
    FlashReplace.replace(divid, src, movieid, width, height, 7, {allowFullScreen:'true', quality:'high', bgcolor:'#000000', scale:'noscale'});
    }

    ps nice script!

  • Robert Nyman - author
    May 28th, 2008 at 15:53

    Ryan,

    Hard to say, but the code looks right. The way the function works is that it replaces the innerHTML of the div you specify with the new value, so theoretically, it should work (although I understand this doesn’t help you much).

    Look for any JavaScript errors and similar things.

  • Ryan
    May 28th, 2008 at 16:14

    yur right its working great now. my script that writes the onClick tags for each thumb was calling the same clip for all thumbs.

    idiotic typo on my part.

    i actually used href=”javascript:loadVideo(.. for a smoother load instead of onClick (no page bouncing)

    thanks for the help!

  • Robert Nyman - author
    May 28th, 2008 at 16:33

    Ryan,

    Good to hear that it works! :-)

  • gary
    May 28th, 2008 at 17:08

    Having problems passing params into the FlashReplace.replace call …

    Trying to add this to the end of the standard args -

    FlashReplace.replace(”frontpageimg”, jsonImgData.images[counter].loc, “flashasset”, 723, 250, {allowscriptaccess: ‘always’, loop: ‘false’});

    Once I add the params, as above, this stops working in FF or IE.

    Without the params, this works as needed in FF; but - surprise - in IE 7, my ExternalInterface.call throws a null. I was using SWF and got around the null bull when adding allowscriptaccess … (?)

    I’ve tried allowScriptAccess, as well …

    Any ideas ?

  • Robert Nyman - author
    May 28th, 2008 at 19:28

    gary,

    Hmm… Weird. The way params work with FlashReplace is that they’re added as param objects in IE and as attributes on the embed object for other web browsers, resulting in HTML that should work (and if you use Firebug, you can easily view the actual generated HTML).

    Looking at Adobe’s documentation, and this is a long shot, but they spell it AllowScriptAccess.

  • gary
    May 28th, 2008 at 21:42

    Wow.

    So … However I was getting around the error, it was *all my own* doing in the first place. ;)

    (Turns out, one should pay attention when using ExternalInterface from a Flash file …)

    Thanks for the code, for your time -
    gary

  • Robert Nyman - author
    May 28th, 2008 at 22:11

    gary,

    No worries. :-)
    Thank you for using FlashReplace!

  • bakkelun
    June 11th, 2008 at 13:34

    Hey Mistah! Back to tell you that this works like a charm, tested in all major browsers (also the sucky ones). Check it out here:

    http://www.dosspirit.net/moduler/flashplayer/player.html

    Wohey!

  • bakkelun
    June 11th, 2008 at 14:18

    Use this URL instead, enjoy =)

    http://www.dosspirit.net/moduler/flashplayer/

  • Robert Nyman - author
    June 11th, 2008 at 14:22

    bakkelun,

    Good to hear! :-)

  • Erin Alicia
    June 21st, 2008 at 1:26

    Most excellent resource, amazed that even I could get it to cook (left/right brain thing).

    Thank you greatly for sharing your knowledge, this code rocks and now I believe I might be on my way to full compliance thanks to you.

    Peace,
    Erin

  • Robert Nyman - author
    June 22nd, 2008 at 19:28

    Erin,

    Great! Good luck!

  • Raza
    June 26th, 2008 at 13:04

    Always refreshing to see honest effort.

    I was wondering if its a good practice to use percentage (%) for the height and width parameters.

    Is this supported by FlashReplace?

  • Robert Nyman - author
    June 26th, 2008 at 18:56

    Raza,

    Thanks. And yes, you can use percentage values as well!

  • Jeffrey Langham
    July 15th, 2008 at 21:42

    Robert,

    Than you for the handy script. It got me over the IE hurdle. Updating the script to produce XHTML was a cinch.

  • Erik
    July 17th, 2008 at 11:35

    After installing Flash player 10 beta, this excellent flash replacement script stopped working.

    Adding a space before the parentheses in the regular expression that checks for the version did the trick.

    So, “/.*(\d+\.\d+).*/” should be “/.* (\d+\.\d+).*/”

    .* was too greedy it seems.

Share your thoughts:

HTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> . If you want to display code examples, please remember to write &lt; for < and &gt; for >.

Comment preview

All the proceeds from ad clicks will go to charity. However, if you like to give something directly to charity yourself, I recommend choosing from the listed ones below.

  • Save the Children
  • Red Cross
  • Cancer Research UK
  • WWF

Top results