4

I'm using Magento 2.4 and i'm trying to use Youtube player API with jQuery in Magento 2 .phtml file like this first:

<script>
    require(['jquery'], function($){
        var tag = document.createElement('script');

        tag.src = "https://www.youtube.com/iframe_api";
        var firstScriptTag = document.getElementsByTagName('script')[0];
        firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
        

However it is not working at all, then I tried to inject the youtube player api script in requirejs-config.js like this:

var config = {
    map: {
        '*':{
            youTubeIFrame: 'https://www.youtube.com/iframe_api?noext'
        }
    }
};

.phtml file:

<script>
    require(['jquery','youTubeIFrame'], function($,YT){...

but the youtube video does not appear at all

Is there a correct way to load the Youtube Player API with jQuery in MAgento 2 ?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.