Open fancybox popup with iframe from a function

Raja picture Raja · Jan 25, 2013 · Viewed 13.5k times · Source

I have searched enough in the Stackoverflow and i could not able to find a thread to answer my question.

Below is my scenario,

function openVideo(videoid,urlid){
        var videourl = 'http://localhost/ptchoice-local/welcome/video/'+videoid+'/'+urlid;

        $("a#video_link").fancybox({
                    'type': 'iframe', 
            'width' : 1000,
            'height' : 600,
            'autoDimensions' : false,
            'autoScale' : false,
            'href' : videourl
        });

    $("a#video_link").trigger('click');
    return false;
} 

<a href="#" onclick="openVideo('2134sdf234532sdfs324234','1')">Show video</a>

So, i would like to set href value when i click the "openVideo" function not in the anchor tag itself.

Fancybox version: jquery.fancybox-1.3.4.pack.js

Please suggest on this.

Answer

areich picture areich · Jan 25, 2013

If I understood your question correctly this should work for you:

Need to add an ID to the anchor:

<a href="#" id="showVideo">Show video</a>

The jQuery:

$("#showVideo").click(function () {
    var videourl = 'http://localhost/ptchoice-local/welcome/video/2134sdf234532sdfs324234/1';

    $("#video_link").fancybox({
        'type': 'iframe', 
        'width' : 1000,
        'height' : 600,
        'autoDimensions' : false,
        'autoScale' : false,
        'href' : videourl
    });

    $("#video_link").trigger('click');
});

Since the videoid and urlid were hard coded in your example, I did the same here. They can be variables if they need be -- just set them the same way.