Fancybox 2.0 and Youtube Video

grabner picture grabner · Apr 12, 2013 · Viewed 14k times · Source

I want to open a youtube video in the new fancybox. I don't get it to work. It always goes directly to the page instead of opening it in the fancybox. Here is the code:

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>youtube video</title>
        <link rel="stylesheet" href="css/jquery.fancybox.css" />
        <script src="./js/jquery-1.9.1.min.js"></script>
        <script src="./js/jquery.fancybox.pack.js"></script>
        <script>
            $(document).ready(function(){
                $(".fancybox-media").fancybox({
                    openEffect  : 'none',
                    closeEffect : 'none',
                    helpers : {
                        media : {}
                    }
                });
            });
        </script>
    </head>
    <body>
        <a href="http://www.youtube.com/watch?v=b_ojoebJyvc" class="fancybox-media">Video</a>
    </body>
</html>

What I'm doing wrong? I also tried this code (as stated on the fancybox website):

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>youtube test</title>
        <link rel="stylesheet" href="css/jquery.fancybox.css" />
        <link rel="stylesheet" href="css/jquery.fancybox-buttons.css" />
        <link rel="stylesheet" href="css/jquery.fancybox-thumbs.css" />


    </head>
    <body>
        <a class="various fancybox.iframe" href="http://www.youtube.com/v/ZeStnz5c2GI?fs=1&amp;autoplay=1"><img src="http://dogmatic69.com/files/portfolio/image/4f288818-c5a8-4872-a003-06376318cd71/stackoverflow-logo.png" width="200" /></a>

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script src="js/jquery.fancybox.pack.js"></script>
        <script src="js/jquery.mousewheel-3.0.6.pack.js"></script>
        <script src="js/jquery.fancybox-buttons.js"></script>
        <script src="js/jquery.fancybox-media.js"></script>
        <script src="js/jquery.fancybox-thumbs.js"></script>

        <script type="text/javascript">
            $(document).ready(function() {
                $(".various").fancybox({
                    maxWidth    : 800,
                    maxHeight   : 600,
                    fitToView   : false,
                    width       : '70%',
                    height      : '70%',
                    autoSize    : false,
                    closeClick  : false,
                    openEffect  : 'elastic',
                    closeEffect : 'none'
                });
            });
        </script>
    </body>
</html>

This code should be the same as on this page. What is the difference?

EDIT:

It is the youtube link!

http://www.youtube.com/v/ZeStnz5c2GI?fs=1&autoplay=1

does work.

This link doesn't

http://www.youtube.com/watch?v=b_ojoebJyvc

What can I do?

$(document).ready(function() {
    $(".various").click(function() {
        $.fancybox({
            maxWidth    : 800,
            maxHeight   : 600,
            fitToView   : false,
            width       : '70%',
            height      : '70%',
            autoSize    : false,
            closeClick  : false,
            openEffect  : 'elastic',
            closeEffect : 'none',
            href        : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/') + '?fs=1&autoplay=1'
        });
        //alert(this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'));
        alert(this.href);
    });
});

This doesn't work. The href is never overwritten!

This also doesn't work (it always follows the link and no fancybox is openend):

$(document).ready(function() {
    $(".various").click(function() {
        $.fancybox({
            maxWidth    : 800,
            maxHeight   : 600,
            fitToView   : false,
            width       : '70%',
            height      : '70%',
            autoSize    : false,
            closeClick  : false,
            openEffect  : 'elastic',
            closeEffect : 'none',
            beforeLoad  : function(){
                var url= $(this.element).data("href");
                url.replace(new RegExp("watch\\?v=", "i"), 'v/') + '?fs=1&autoplay=1'
                this.href = url
                alert(url);
            }
        });
        //alert(this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'));
        //alert(this.href);
    });
});

So I think you can forget fancybox 2.0. Take the old one and you are good ...

Answer

grabner picture grabner · Apr 12, 2013

Thanks to JFK again. This works now:

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>youtube test</title>
        <link rel="stylesheet" href="css/jquery.fancybox.css" />
    </head>
    <body>
        <a class="various fancybox.iframe" href="http://www.youtube.com/watch?v=L9szn1QQfas"><img src="http://dogmatic69.com/files/portfolio/image/4f288818-c5a8-4872-a003-06376318cd71/stackoverflow-logo.png" width="200" /></a>

        <script src="js/jquery-1.9.1.min.js"></script>
        <script src="js/jquery.fancybox.pack.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $(".various").fancybox({
                    maxWidth    : 800,
                    maxHeight   : 600,
                    fitToView   : false,
                    width       : '70%',
                    height      : '70%',
                    autoSize    : false,
                    closeClick  : false,
                    openEffect  : 'elastic',
                    closeEffect : 'none',
                    beforeLoad  : function(){
                        var url= $(this.element).attr("href");
                        url = url.replace(new RegExp("watch\\?v=", "i"), 'v/');
                        url += '?fs=1&autoplay=1';
                        this.href = url
                    }
                });
            });
        </script>
    </body>
</html>