2

I want to change the src attribute of an embed object. I have:

<embed class="flash "id="flash" src="swf/pano.swf?panoSrc=images/a.jpg" 
                     allowFullScreen="true" 
                     width="1280" height="640" quality="high" 
                     pluginspage="http://www.macromedia.com/go/getflashplayer" 
                     type="application/x-shockwave-flash" bgcolor="#DDDDDD"/>

and I've tried

function change() 
{
   document.getElementById("flash").setAttribute('src', 'swf/pano.swf?panoSrc=b.jpg');
}

but it didn't work in Chrome, "only" in Firefox, IE and Opera. How can I fix it (to make it work in Chrome)? Did I do anything wrong?

EDIT: here is the whole HTML file

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script>
        function change() {
            document.getElementById("flash").setAttribute('src', 'swf/pano.swf?panoSrc=a.jpg');
        }
    </script>

</head>
<body>


    <embed class="flash" id="flash" src="swf/pano.swf?panoSrc=images/sau.jpg" allowFullScreen="true" 
                     width="1280" height="640" quality="high" 
                     pluginspage="http://www.macromedia.com/go/getflashplayer" 
                     type="application/x-shockwave-flash" bgcolor="#DDDDDD"/>

     <script>
         change();
     </script>

</body>

Link to the page: http://chin.comli.com/test.html

Chin
  • 19,717
  • 37
  • 107
  • 164

3 Answers3

3

You need to invoke your function.

change();

Working demo: http://jsfiddle.net/LUfUx/

AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
2

Move your script call to be AFTER the code it's intended to affect, or put it in a window.onload call. As you have it it's trying to execute on an element before it exists.

Long story short, based on Changing object embed source via jquery, Dynamically change embedded video src in IE/Chrome (works in Firefox), JavaScript: Changing src-attribute of a embed-tag, and http://code.google.com/p/chromium/issues/detail?id=69648, it looks like this is a bug in Chrome and the only workaround is to remove the entire <embed> element, and then re-insert it with the changed src.

Community
  • 1
  • 1
j08691
  • 204,283
  • 31
  • 260
  • 272
  • I see. But even after doing that it still doesn't work. I edited the code. Is that what you mean? – Chin Oct 19 '12 at 00:29
  • Don't use webmatrix to fix this, debug it in Chrome or Firefox and use the built in dev tools or a plugin to see any errors. As you can see by this fiddle, http://jsfiddle.net/j08691/AJDuY/, the code works, so there's something else going on. – j08691 Oct 19 '12 at 00:32
  • oh, I just found out that it works in firefox, but not in Chrome, and I always use Chrome to preview the result. Pretty weird... – Chin Oct 19 '12 at 00:37
  • how can I use the dev's tool in Chrome? Sorry I'm a total newbie – Chin Oct 19 '12 at 00:42
  • Hit F12 to open them and choose the console. Then reload the page. – j08691 Oct 19 '12 at 00:43
  • After reloading the page the console is still blank and doesn't show anything – Chin Oct 19 '12 at 00:44
  • Is your page public? If it works in most other browsers it should work in Chrome. Try clearing the cache etc and restarting Chrome. – j08691 Oct 19 '12 at 00:46
  • Here is the link:http://chin.comli.com/test.html You can try it in chrome and firefox to see the difference – Chin Oct 19 '12 at 01:35
  • This may be more of a flash embed code issue than a script issue. The code runs fine in Chrome and changes the source but it appears as if the embed isn't working properly. – j08691 Oct 19 '12 at 01:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18266/discussion-between-chin-and-j08691) – Chin Oct 19 '12 at 01:40
0

Try this

function change() {
   document.getElementById("flash").src =  'swf/pano.swf?panoSrc=b.jpg';
   // OR  document["flash"].src =  'swf/pano.swf?panoSrc=b.jpg';
}
Sushanth --
  • 55,259
  • 9
  • 66
  • 105