Posted By

kristapsancans on 02/17/11


Tagged

javascript object post flash video embed blog content jquery replacement YouTube


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

jondiscipulo
Balamir
wirenaught


jQuery: finding all youtube.com links and replacing them with flash video


 / Published in: jQuery
 

URL: http://www.fyfi.net

Easy way to replace all youtube.com links with embed flash video on page or blog post/posts.

Go to youtube.com, find the video (for example http://www.youtube.com/watch?v=zzk-8akrWxo), copy link and paste it in your page/blog post.

Change in script width and height to your desired parameters and change '#blog .post' to your page content container or blog post container.

Live preview can be seen here - http://motorolleri.lv/blogs/

That's it :) Enjoy.

  1. $(function(){
  2. var vidWidth = 459; // youtbe video width
  3. var vidHeight = 283; // youtube video height
  4. var obj = '<object width="' + vidWidth + '" height="' + vidHeight + '">' +
  5. '<param name="movie" value="http://www.youtube.com/v/[vid]&amp;hl=en&amp;fs=1">' +
  6. '</param><param name="allowFullScreen" value="true"></param><param ' +
  7. 'name="allowscriptaccess" value="always"></param>' +
  8. '<param name="wmode" value="transparent"></param><em' +
  9. 'bed src="http://www.youtube.com/v/[vid]&amp;hl=en&amp;fs=1" ' +
  10. 'type="application/x-shockwave-flash" wmode="transparent" allowscriptaccess="always" ' +
  11. 'allowfullscreen="true" width="' + vidWidth + '" ' + 'height="' +
  12. vidHeight + '"></embed></object>';
  13.  
  14. $('#blog .post').find('p:contains("youtube.com/watch")').each(function(){
  15. var that = $(this);
  16. var vid = that.html().match(/(?:v=)([\w\-]+)/g);
  17. if (vid.length) {
  18. $.each(vid, function(i){
  19. that.replaceWith( obj.replace(/\[vid\]/g, this.replace('v=','')) );
  20. });
  21. }
  22. });
  23. });

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: BebenKoben on February 17, 2011

its cool...thanks for script my brooo \m/

You need to login to post a comment.