How do I dynamically add &autoplay=1 to youtube embed codes?
This is an examples of the youtube embed code on my site:
<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 believe I need to add &autoplay=1
to 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="32开发者_StackOverflow0" allowscriptaccess="always" allowfullscreen="true" wmode="opaque">
I need to do this dynamically with JQuery. How would I do this?
Without too 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 prefer to do a slightly more specific selector, for instance $('embed[src^="http://www.youtube.com/"]')
or perhaps $('object param[name="movie"]')
-- it depends on what your page contains elsewhere...
See the API reference:
attribute-equals
selectorval
prop
To play a YouTube movie by JavaScript, you'll better use the JavaScript Player API:
http://code.google.com/intl/nl/apis/youtube/js_api_reference.html
(Yes I know this is not precisely the answer to the OP's question).
How to auto-play youtube link?
1) Use the code and simply 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 the &autoplay=0 to click and play manually!
精彩评论