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

我怕爱的太早我们不能终老 提交于 2019-12-23 18:32:25

问题


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="320" allowscriptaccess="always" allowfullscreen="true" wmode="opaque">

I need to do this dynamically with JQuery. How would I do this?


回答1:


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 selector
  • val
  • prop



回答2:


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).




回答3:


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!



来源:https://stackoverflow.com/questions/6144926/how-do-i-dynamically-add-autoplay-1-to-youtube-embed-codes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!