How can I dynamically add & autoplay = 1 to youtube embed codes?

These are examples of code for embedding youtube on my website:

<object width="525" height="320"><param name="wmode" value="opaque">
    <param name="movie" value="http://www.youtube.com/v/yciTqWafKPU?version=3">
    <param name="allowFullScreen" value="true">
    <param name="allowscriptaccess" value="always">
    <embed src="http://www.youtube.com/v/yciTqWafKPU?version=3" type="application/x-shockwave-flash" width="525" height="320" allowscriptaccess="always" allowfullscreen="true" wmode="opaque">
</object>

I think I need to add &autoplay=1to this: <param name="movie" value="http://www.youtube.com/v/yciTqWafKPU?version=3&autoplay=1">and this:<embed src="http://www.youtube.com/v/yciTqWafKPU?version=3&autoplay=1" type="application/x-shockwave-flash" width="525" height="320" allowscriptaccess="always" allowfullscreen="true" wmode="opaque">

I need to do this dynamically using jQuery. How should I do it?

+3
source share
3 answers

Without much difficulty, I would have thought ...

$('param[name="movie"]').val(function(i, oldVal) {
    return oldVal + (oldVal.indexOf('?') ? '&autoplay=1' : '?autoplay=1');
});
$('embed').prop('src', function(i, oldSrc) {
    return oldSrc + (oldSrc.indexOf('?') ? '&autoplay=1' : '?autoplay=1');
}

You might want to make a slightly more specific selector, for example, $('embed[src^="http://www.youtube.com/"]')or perhaps $('object param[name="movie"]')it depends on what your page contains elsewhere ...

See the API link:

+4
source

To play a YouTube video using JavaScript, you'd better use the JavaScript Player API:

http://code.google.com/intl/nl/apis/youtube/js_api_reference.html

(, , OP).

0

YouTube?

1) Use the code and just change the URL to http://www.youtube.com/v/ ? version = 3 & autoplay = 1

<object width="525" height="320"><param name="wmode" value="opaque">
    <param name="movie" value="http://www.youtube.com/v/yciTqWafKPU?version=3">
    <param name="allowFullScreen" value="true">
    <param name="allowscriptaccess" value="always">
    <embed src="http://www.youtube.com/v/yciTqWafKPU?version=3" type="application/x-shockwave-flash" width="525" height="320" allowscriptaccess="always" allowfullscreen="true" wmode="opaque">
</object>

2) remove & autoplay = 0 to click and play manually!

0
source

All Articles