Change Vimeo Video using JQuery
-
23-10-2019 - |
Question
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&
server=vimeo.com&show_title=1&show_byline=0&show_portrait=0&color=00ADEF&fullscreen=1" />
<embed src="http://vimeo.com/moogaloop.swf?clip_id=11527784&
server=vimeo.com&show_title=1&show_byline=0&show_portrait=0&
color=00ADEF&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
Solution
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
OTHER TIPS
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: How to use javascript to swap swf in html?
var params = document.getElementsByTagName("param");
var clip_url = 11527780;
var vimeo = "http://vimeo.com/moogaloop.swf?clip_id=" + clip_url + "&server=vimeo.com&show_title=1&show_byline=0&show_portrait=0&color=00ADEF&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.