question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

youtube link on magnific popup is not autoplaying

See original GitHub issue

On clicking the link the popup shows up with video - but does not autoplay it. Plays only on clicking again the popped up youtube button. I tried with different options for autoplay. Any help would be much appreciated!

This is the link

<a href="https://www.youtube.com/watch?v=8Shndkrgc88" class="video" >

With this script

$('.popup-gallery').magnificPopup({
    		delegate: 'a',
    		type: 'image',
        callbacks: {
          elementParse: function(item) {
            // Function will fire for each target element
            // "item.el" is a target DOM element (if present)
            // "item.src" is a source that you may modify
            // console.log(item.el.context.className);
            if(item.el[0].className == 'video') {
              item.type = 'iframe',
              item.iframe = {
                 patterns: {
                   youtube: {
                     index: 'youtube.com/', 
                     id: 'v=', 
                     src: '//www.youtube.com/embed/%id%?autoplay=1&mute=1' 
                   },
                   vimeo: {
                     index: 'vimeo.com/',
                     id: '/',
                     src: '//player.vimeo.com/video/%id%?autoplay=1'
                   },
                   gmaps: {
                     index: '//maps.google.',
                     src: '%id%&output=embed'
                   }
                 }
              }
            } else {
               item.type = 'image',
               item.tLoading = 'Loading image #%curr%...',
               item.mainClass = 'mfp-img-mobile',
               item.image = {
                 tError: '<a href="%url%">The image #%curr%</a> could not be loaded.'
               }
            }

          }
        },
    		gallery: {
    			enabled: true,
    			navigateByImgClick: true,
    			preload: [0,1] // Will preload 0 - before current, and 1 after the current image
    		}
        
    	});`

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

18reactions
apmeyercommented, May 28, 2020

You shouldn’t need to edit any core files. I found that in addition to including options to reformat the URL, you also need to include the option to modify the iframe markup. Make sure to include allow="autoplay" on the opening iframe tag.

{
	type: 'iframe',
	iframe: {
		markup: '<div class="mfp-iframe-scaler">' +
			'<div class="mfp-close"></div>' +
			'<iframe class="mfp-iframe" frameborder="0" allow="autoplay"></iframe>' +
			'</div>',
		patterns: {
			youtube: {
				index: 'youtube.com/',
				id: 'v=',
				src: 'https://www.youtube.com/embed/%id%?autoplay=1'
			}
		}
	}
}
5reactions
sivasankarankbcommented, Jul 4, 2019

Changing this :

https://github.com/dimsemenov/Magnific-Popup/blob/c8f6b8549ebff2306c5f1179c9d112308185fe2c/src/js/iframe.js#L27

To this worked for me:

'<iframe class="mfp-iframe" src="//about:blank" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>'+

This should also be OK:

'<iframe class="mfp-iframe" src="//about:blank" frameborder="0" allow="autoplay" allowfullscreen></iframe>'+

(YouTube embed code comes with the first version.)

Also had to change this:

https://github.com/dimsemenov/Magnific-Popup/blob/c8f6b8549ebff2306c5f1179c9d112308185fe2c/src/js/iframe.js#L37

to this:

src: 'https://www.youtube.com/embed/%id%?autoplay=1'

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rel=0 and autoplay=1 in YouTube embed player in magnific ...
None of them are working. But if I move autoplay=1 up as the first parameter, then the autoplay works fine - but then...
Read more >
magnific popup youtube autoplay - CodePen
Insecure Resource. The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https....
Read more >
Start Video When the Popup Opens
The following custom code examples show how you can auto-play videos when your video popup opens. Note: Replace the example popup ID (123)...
Read more >
Add “&rel=0” to popup YouTube videos - WordPress.org
js to hard code that query parameter. So you look for “//www.youtube.com/embed/%id%?autoplay=1” and simply add the “&rel=0” ...
Read more >
Magnific Popup Documentation - Dmitry Semenov
Magnific Popup core CSS file --> <link rel="stylesheet" ... src: '//www.youtube.com/embed/%id%?autoplay=1' // URL that will be set as a source for iframe.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found