Search code examples
javascriptvisual-studio-2010swfobject

JavaScript Not working SWFObject is undefined


I am getting this error "Microsoft JScript runtime error: 'SWFObject' is undefined"

my code looks like this

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>   
    <div id="flashcontent">This text is replaced by the Flash movie. </div>   
<script type="text/javascript">
    var rndPick = 2;
    var rndPick = Math.floor(Math.random() * 16) + 1;
    var movie = "/Flash/sam" + rndPick + ".swf";
    var so = new SWFObject(movie, "mymovie", "955", "170", "8", "#336699");
    so.write("flashcontent");
    setTimeout("location.reload(true);", 14500);  
</script>  

Solution

  • You're using SWFObject 1.5 syntax, but linking to the SWFObject 2.2 JS file. SWFObject 1.5 and 2.2 are incompatible.

    Rewrite your SWFObject code to use the 2.2 syntax. Here is your code converted to SWFObject 2.2 syntax. Note that swfobject.embedSWF is automatically executed when the DOM has finished loading.

    <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>   
    
    <script type="text/javascript">
    var rndPick = Math.floor(Math.random() * 16) + 1;
    var movie = "/Flash/sam" + rndPick + ".swf";
    
    var flashvars = {}; //empty for this example
    var params = { bgcolor: "#336699" };  //sets background color
    var attributes = { id: "mymovie" }; //sets ID of <object> to "mymovie"
    
    //Optional callback function gets executed after <object> is created
    var callbackFn = function (){
        setTimeout("location.reload(true);", 14500);
    };
    
    swfobject.embedSWF(movie, "flashcontent", "955", "170", "8", false, flashvars, params, attributes, callbackFn);
    
    </script> 
    </head>
    
    <body>
        <div id="flashcontent">This text is replaced by the Flash movie. </div>   
    </body>