views:

454

answers:

3

Hello all,

How can I change the ID of the embedded vimeo video? Here is the embed code for example:

<object width="578" height="325">
<param name="allowfullscreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=11527784&amp;amp;
server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" />
<embed src="http://vimeo.com/moogaloop.swf?clip_id=11527784&amp;amp;
server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;
color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" 
allowscriptaccess="always" width="578" height="325"></embed></object>

How can I change the clip_id in both the object value and the embed source using JQuery or just pure javascript?

The effect this has is that it changes the video. I have tested this on Firefox, if this won't work on all browsers please let me know!

Thanks all for any help

+1  A: 

I am not sure "when" you want to replace it, so I made it on click. Try this:

$('#change_me').click(function() {
    var val = $('embed').attr('src').replace(/clip_id=(?=\d).\d+/g, 'clip_id='+new_id);


    $('embed').attr('src', val);
    alert($('embed').attr('src'));
});

This regular expression will find clip_id followed by any numbers and only numbers and replace them with the new id. You can do this on both urls that contain clip_id. I am pretty sure it will work on all browsers, although I cant test it right now.

Note: I am sure that a better regular expression could be used, but I am not particulary good with them, so thats all I can come up with atm.

Hope this helps

realshadow
What is the selector: `$('embed')` it doesn't seem to work with that selector?
Abs
It will select the <embed></embed> tag, although, if you have more flash movies on your site, it will select the first one on your site, so you will have to change the selector to work with your code, e.g. you would use $('param[name="movie"]') to select the clip_id in the param tag. Prolly the best way would be to wrap it on some div.
realshadow
+1  A: 

There are some cross-browser implications for this.

You might want to take a look at another question on SO.

You should use the SWFObject plugin for this as stated by SolutionYogi.

Here's another related question: http://stackoverflow.com/questions/291647/how-to-use-javascript-to-swap-swf-in-html

sirhc
+1 for taking into consideration the implications.
Pekka
@Sirhc - Thank you for the reply. What are the cross browser implications that you speak of?
Abs
Loading Flash files in older browsers can be rather tricky. I read somewhere that you have to reload the object element into the DOM in order for it load. I can't find a link to that at the moment though. Meanwhile, there's a list of reasons to use SWFObject over [at this forum thread](http://www.webmasterworld.com/video/3792156.htm).
sirhc
A: 
    var params = document.getElementsByTagName("param");
    var clip_url = 11527780;
    var vimeo = "http://vimeo.com/moogaloop.swf?clip_id=" + clip_url + "&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1";
    params[2].setAttribute("value", vimeo);

    var embed = document.getElementsByTagName("embed");
    embed[0].setAttribute("src", vimeo);

clip_url is the id of the vimeo video.

Wai Wong
Worked like a treat in firefox but tried it on IE8 and I get this error: `Message: '2' is null or not an object`.
Abs
It says on row 18 that is the error. I am not quite sure what it could be. I am trying this on other browsersEdit: It seems not to work with Chrome either. I guess the DOM code doesnt work for embeddded videos
Wai Wong