{"id":652,"date":"2023-09-18T05:29:00","date_gmt":"2023-09-18T03:29:00","guid":{"rendered":"https:\/\/blog.pirulug.com\/?p=652"},"modified":"2023-09-18T05:29:00","modified_gmt":"2023-09-18T03:29:00","slug":"bootstrap-que-comparta-un-video-de-youtube-y-que-al-cerrarse-el-modal-reinicie-el-iframe-de-youtube","status":"publish","type":"post","link":"https:\/\/blog.pirulug.com\/?p=652","title":{"rendered":"Bootstrap que comparta un video de YouTube, y que al cerrarse el modal, reinicie el iframe de YouTube"},"content":{"rendered":"<p class=\"has-line-data\" data-line-start=\"0\" data-line-end=\"1\">\n  Aqu\u00ed tienes un ejemplo de c\u00f3mo crear un modal con Bootstrap que comparta un<br \/>\n  video de YouTube, y que al cerrarse el modal, reinicie el iframe de YouTube:\n<\/p>\n<pre><code class=\"has-line-data\" data-line-start=\"3\" data-line-end=\"68\">&lt;span class=&quot;hljs-doctype&quot;&gt;&lt;!DOCTYPE html&gt;&lt;\/span&gt;\n&lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;html&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;lang&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;en&quot;&lt;\/span&gt;&gt;&lt;\/span&gt;\n&lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;head&lt;\/span&gt;&gt;&lt;\/span&gt;\n&lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;meta&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;charset&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;UTF-8&quot;&lt;\/span&gt;&gt;&lt;\/span&gt;\n&lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;meta&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;name&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;viewport&quot;&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;content&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;width=device-width, initial-scale=1.0&quot;&lt;\/span&gt;&gt;&lt;\/span&gt;\n&lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;title&lt;\/span&gt;&gt;&lt;\/span&gt;Modal con reinicio de iframe de YouTube (JavaScript Vanilla)&lt;span class=&quot;hljs-tag&quot;&gt;&lt;\/&lt;span class=&quot;hljs-title&quot;&gt;title&lt;\/span&gt;&gt;&lt;\/span&gt;\n&lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;link&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;rel&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;stylesheet&quot;&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;href&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/5.3.1\/css\/bootstrap.min.css&quot;&lt;\/span&gt;&gt;&lt;\/span&gt;\n&lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;script&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;src&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/5.3.1\/js\/bootstrap.min.js&quot;&lt;\/span&gt;&gt;&lt;\/span&gt;&lt;span class=&quot;undefined&quot;&gt;&lt;\/span&gt;&lt;span class=&quot;hljs-tag&quot;&gt;&lt;\/&lt;span class=&quot;hljs-title&quot;&gt;script&lt;\/span&gt;&gt;&lt;\/span&gt;\n&lt;span class=&quot;hljs-tag&quot;&gt;&lt;\/&lt;span class=&quot;hljs-title&quot;&gt;head&lt;\/span&gt;&gt;&lt;\/span&gt;\n&lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;body&lt;\/span&gt;&gt;&lt;\/span&gt;\n\n&lt;span class=&quot;hljs-comment&quot;&gt;&lt;!-- Bot\u00f3n que activa el modal --&gt;&lt;\/span&gt;\n&lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;button&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;type&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;button&quot;&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;class&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;btn btn-primary&quot;&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;data-toggle&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;modal&quot;&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;data-target&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;#myModal&quot;&lt;\/span&gt;&gt;&lt;\/span&gt;\n  Abrir Modal\n&lt;span class=&quot;hljs-tag&quot;&gt;&lt;\/&lt;span class=&quot;hljs-title&quot;&gt;button&lt;\/span&gt;&gt;&lt;\/span&gt;\n\n&lt;span class=&quot;hljs-comment&quot;&gt;&lt;!-- Modal --&gt;&lt;\/span&gt;\n&lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;div&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;class&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;modal fade&quot;&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;id&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;myModal&quot;&lt;\/span&gt;&gt;&lt;\/span&gt;\n  &lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;div&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;class&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;modal-dialog&quot;&lt;\/span&gt;&gt;&lt;\/span&gt;\n    &lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;div&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;class&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;modal-content&quot;&lt;\/span&gt;&gt;&lt;\/span&gt;\n    \n      &lt;span class=&quot;hljs-comment&quot;&gt;&lt;!-- Modal Header --&gt;&lt;\/span&gt;\n      &lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;div&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;class&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;modal-header&quot;&lt;\/span&gt;&gt;&lt;\/span&gt;\n        &lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;h4&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;class&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;modal-title&quot;&lt;\/span&gt;&gt;&lt;\/span&gt;Video de YouTube&lt;span class=&quot;hljs-tag&quot;&gt;&lt;\/&lt;span class=&quot;hljs-title&quot;&gt;h4&lt;\/span&gt;&gt;&lt;\/span&gt;\n        &lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;button&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;type&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;button&quot;&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;class&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;close&quot;&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;data-dismiss&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;modal&quot;&lt;\/span&gt;&gt;&lt;\/span&gt;&amp;times;&lt;span class=&quot;hljs-tag&quot;&gt;&lt;\/&lt;span class=&quot;hljs-title&quot;&gt;button&lt;\/span&gt;&gt;&lt;\/span&gt;\n      &lt;span class=&quot;hljs-tag&quot;&gt;&lt;\/&lt;span class=&quot;hljs-title&quot;&gt;div&lt;\/span&gt;&gt;&lt;\/span&gt;\n      \n      &lt;span class=&quot;hljs-comment&quot;&gt;&lt;!-- Modal body --&gt;&lt;\/span&gt;\n      &lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;div&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;class&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;modal-body&quot;&lt;\/span&gt;&gt;&lt;\/span&gt;\n        &lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;div&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;class&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;embed-responsive embed-responsive-16by9&quot;&lt;\/span&gt;&gt;&lt;\/span&gt;\n          &lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;iframe&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;class&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;embed-responsive-item&quot;&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;id&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;youtubeIframe&quot;&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;allowfullscreen&lt;\/span&gt;&gt;&lt;\/span&gt;&lt;span class=&quot;hljs-tag&quot;&gt;&lt;\/&lt;span class=&quot;hljs-title&quot;&gt;iframe&lt;\/span&gt;&gt;&lt;\/span&gt;\n        &lt;span class=&quot;hljs-tag&quot;&gt;&lt;\/&lt;span class=&quot;hljs-title&quot;&gt;div&lt;\/span&gt;&gt;&lt;\/span&gt;\n      &lt;span class=&quot;hljs-tag&quot;&gt;&lt;\/&lt;span class=&quot;hljs-title&quot;&gt;div&lt;\/span&gt;&gt;&lt;\/span&gt;\n      \n      &lt;span class=&quot;hljs-comment&quot;&gt;&lt;!-- Modal footer --&gt;&lt;\/span&gt;\n      &lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;div&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;class&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;modal-footer&quot;&lt;\/span&gt;&gt;&lt;\/span&gt;\n        &lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;button&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;type&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;button&quot;&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;class&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;btn btn-secondary&quot;&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;data-dismiss&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;modal&quot;&lt;\/span&gt;&gt;&lt;\/span&gt;Cerrar&lt;span class=&quot;hljs-tag&quot;&gt;&lt;\/&lt;span class=&quot;hljs-title&quot;&gt;button&lt;\/span&gt;&gt;&lt;\/span&gt;\n      &lt;span class=&quot;hljs-tag&quot;&gt;&lt;\/&lt;span class=&quot;hljs-title&quot;&gt;div&lt;\/span&gt;&gt;&lt;\/span&gt;\n      \n    &lt;span class=&quot;hljs-tag&quot;&gt;&lt;\/&lt;span class=&quot;hljs-title&quot;&gt;div&lt;\/span&gt;&gt;&lt;\/span&gt;\n  &lt;span class=&quot;hljs-tag&quot;&gt;&lt;\/&lt;span class=&quot;hljs-title&quot;&gt;div&lt;\/span&gt;&gt;&lt;\/span&gt;\n&lt;span class=&quot;hljs-tag&quot;&gt;&lt;\/&lt;span class=&quot;hljs-title&quot;&gt;div&lt;\/span&gt;&gt;&lt;\/span&gt;\n\n&lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;script&lt;\/span&gt;&gt;&lt;\/span&gt;&lt;span class=&quot;javascript&quot;&gt;\n&lt;span class=&quot;hljs-built_in&quot;&gt;document&lt;\/span&gt;.addEventListener(&lt;span class=&quot;hljs-string&quot;&gt;&#039;DOMContentLoaded&#039;&lt;\/span&gt;, &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;\/span&gt;(&lt;span class=&quot;hljs-params&quot;&gt;&lt;\/span&gt;) &lt;\/span&gt;{\n  &lt;span class=&quot;hljs-comment&quot;&gt;\/\/ Obtener el iframe y la URL del video&lt;\/span&gt;\n  &lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;\/span&gt; iframe = &lt;span class=&quot;hljs-built_in&quot;&gt;document&lt;\/span&gt;.getElementById(&lt;span class=&quot;hljs-string&quot;&gt;&#039;youtubeIframe&#039;&lt;\/span&gt;);\n  &lt;span class=&quot;hljs-keyword&quot;&gt;var&lt;\/span&gt; youtubeUrl = &lt;span class=&quot;hljs-string&quot;&gt;&#039;URL_DEL_VIDEO_DE_YOUTUBE&#039;&lt;\/span&gt;; &lt;span class=&quot;hljs-comment&quot;&gt;\/\/ Reemplaza esto con la URL del video de YouTube&lt;\/span&gt;\n  \n  &lt;span class=&quot;hljs-comment&quot;&gt;\/\/ Funci\u00f3n para cargar el video de YouTube en el iframe al abrir el modal&lt;\/span&gt;\n  &lt;span class=&quot;hljs-built_in&quot;&gt;document&lt;\/span&gt;.getElementById(&lt;span class=&quot;hljs-string&quot;&gt;&#039;myModal&#039;&lt;\/span&gt;).addEventListener(&lt;span class=&quot;hljs-string&quot;&gt;&#039;show.bs.modal&#039;&lt;\/span&gt;, &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;\/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;&lt;\/span&gt;) &lt;\/span&gt;{\n    iframe.src = youtubeUrl;\n  });\n\n  &lt;span class=&quot;hljs-comment&quot;&gt;\/\/ Funci\u00f3n para reiniciar el iframe al cerrar el modal&lt;\/span&gt;\n  &lt;span class=&quot;hljs-built_in&quot;&gt;document&lt;\/span&gt;.getElementById(&lt;span class=&quot;hljs-string&quot;&gt;&#039;myModal&#039;&lt;\/span&gt;).addEventListener(&lt;span class=&quot;hljs-string&quot;&gt;&#039;hidden.bs.modal&#039;&lt;\/span&gt;, &lt;span class=&quot;hljs-function&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;function&lt;\/span&gt; (&lt;span class=&quot;hljs-params&quot;&gt;&lt;\/span&gt;) &lt;\/span&gt;{\n    iframe.src = &lt;span class=&quot;hljs-string&quot;&gt;&#039;&#039;&lt;\/span&gt;;\n  });\n});\n&lt;\/span&gt;&lt;span class=&quot;hljs-tag&quot;&gt;&lt;\/&lt;span class=&quot;hljs-title&quot;&gt;script&lt;\/span&gt;&gt;&lt;\/span&gt;\n\n&lt;span class=&quot;hljs-tag&quot;&gt;&lt;\/&lt;span class=&quot;hljs-title&quot;&gt;body&lt;\/span&gt;&gt;&lt;\/span&gt;\n&lt;span class=&quot;hljs-tag&quot;&gt;&lt;\/&lt;span class=&quot;hljs-title&quot;&gt;html&lt;\/span&gt;&gt;&lt;\/span&gt;\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Aqu\u00ed tienes un ejemplo de c\u00f3mo crear un modal con Bootstrap que comparta un video de YouTube, y que al cerrarse el modal, reinicie el iframe de YouTube: &lt;span class=&quot;hljs-doctype&quot;&gt;&lt;!DOCTYPE html&gt;&lt;\/span&gt; &lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;html&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;lang&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;en&quot;&lt;\/span&gt;&gt;&lt;\/span&gt; &lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;head&lt;\/span&gt;&gt;&lt;\/span&gt; &lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;meta&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;charset&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;UTF-8&quot;&lt;\/span&gt;&gt;&lt;\/span&gt; &lt;span class=&quot;hljs-tag&quot;&gt;&lt;&lt;span class=&quot;hljs-title&quot;&gt;meta&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;name&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;viewport&quot;&lt;\/span&gt; &lt;span class=&quot;hljs-attribute&quot;&gt;content&lt;\/span&gt;=&lt;span class=&quot;hljs-value&quot;&gt;&quot;width=device-width, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-652","post","type-post","status-publish","format-standard","hentry","category-sin-categoria"],"_links":{"self":[{"href":"https:\/\/blog.pirulug.com\/index.php?rest_route=\/wp\/v2\/posts\/652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.pirulug.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.pirulug.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.pirulug.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.pirulug.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=652"}],"version-history":[{"count":0,"href":"https:\/\/blog.pirulug.com\/index.php?rest_route=\/wp\/v2\/posts\/652\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.pirulug.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.pirulug.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.pirulug.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}