views:

444

answers:

2

Here is my HTML:

<a class="video iframe" href="http://www.youtube.com/watch?v=Psk2Pq03rv0&amp;#38;fs=1"&gt;Arbitrary text</a>

Here is the Fancybox javascript:

<script type='text/javascript'>
$(document).ready(function(){
$("a.video").fancybox({
'href' : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
'type' : 'swf',
'swf' : {
'wmode': 'transparent',
'allowfullscreen': 'true'
}
});
return false;
});

Firebug Console says:

this.href is undefined
'href' : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),

As a result, clicking this link takes the user to YouTube and does not trigger Fancybox. Changing the problematic line to 'href' : this.href.replace(new RegExp("watch?v=", "i"), 'v/'), which seems more correct to me, yields the same result.

Any advice?

EDIT: I adjusted my script to only include the parts relevant to my question.

+1  A: 

Script tag should look like this

<script type='text/javascript'>
$(document).ready(function(){
    $("a.video").click(function() {
        $.fancybox({
                'padding' : 0,
                'autoScale' : false,
                'title' : this.title,
                'overlayOpacity' : '.6',
                'overlayColor' : '#333',
                'transitionIn' : 'none',
                'transitionOut' : 'none',
                'centerOnScroll' : false,
                'showCloseButton' : true,
                'hideOnOverlayClick': false,
                'href' : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
                'type' : 'swf',
                'swf' : {
                'wmode': 'transparent',
                'allowfullscreen': 'true'
                }
        });
        return false;
    });
});
</script>

url should look like this - add &fs=1 to the end of it

<a class="video iframe" href="http://www.youtube.com/watch?v=Psk2Pq03rv0&amp;fs=1"&gt;Arbitrary text</a>
wowo_999
Well I sure wish I knew how to make my code pretty and structured like yours, but oh well. I apologize for not including the `$(document).ready` earlier - I thought that much would be assumed.The ``, which I neglected to replace after I moved this code over from another file, did help a little. Fancybox is triggered now, but the box is white and empty; the video never loads. Another stab at it?I've updated my code appropriately.
danfo
$("a.video").click(function() { $.fancybox({your code is still missing this part, and its important, its what makes the 'this' in the fancybox options work
wowo_999
Thanks wowo, but the link still goes to YouTube without triggering Fancybox. Here's what my code looks like at this moment, excluding all the unnecessary style properties:`$(document).ready(function() { $("a.video").click(function() { $.fancybox({ 'href' : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'), 'type' : 'swf', 'swf' : { 'wmode': 'transparent', 'allowfullscreen': 'true' } }); }); return false; });`
danfo
move your return false up 1 line, it should be inside of the .click function like so.click(function(){init fancy box ... return false;}
wowo_999
I tried it both ways, same result.
danfo
I'm an idiot. I had changed the class name and didn't update it [again] when I copy/pasted your code. This works wonderfully. Thanks so much for your time!
danfo
+1  A: 

this.href, in that context, is referring to document, not the anchor.

$("a.video").fancybox({
    'padding' : 0,
    'autoScale' : false,
    'title' : this.title,
    'overlayOpacity' : '.6',
    'overlayColor' : '#333',
    'transitionIn' : 'none',
    'transitionOut' : 'none',
    'centerOnScroll' : false,
    'showCloseButton' : true,
    'hideOnOverlayClick': false,
    'href' : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
    'type' : 'swf',
    'swf' : {
        'wmode': 'transparent',
        'allowfullscreen': 'true'
    }
});

Is equally written as:

var options = {
    'padding' : 0,
    'autoScale' : false,
    'title' : this.title,
    'overlayOpacity' : '.6',
    'overlayColor' : '#333',
    'transitionIn' : 'none',
    'transitionOut' : 'none',
    'centerOnScroll' : false,
    'showCloseButton' : true,
    'hideOnOverlayClick': false,
    'href' : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
    'type' : 'swf',
    'swf' : {
        'wmode': 'transparent',
        'allowfullscreen': 'true'
    }
};
$("a.video").fancybox(options);

the anchor is not available in that context.

One option is to wrap the code in an each block

$("a.video").each(function(function(index, value)) {
   var obj = $(value);
   obj.fancybox({
        'padding' : 0,
        'autoScale' : false,
        'title' : this.title,
        'overlayOpacity' : '.6',
        'overlayColor' : '#333',
        'transitionIn' : 'none',
        'transitionOut' : 'none',
        'centerOnScroll' : false,
        'showCloseButton' : true,
        'hideOnOverlayClick': false,
        'href' : value.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
        'type' : 'swf',
        'swf' : {
            'wmode': 'transparent',
            'allowfullscreen': 'true'
        }
    });
});
halkeye
This makes good sense, but there's a syntax error in your first line, can you spot it? "Missing formal parameter" is the error message - I'm not overly familiar with Javascript syntax, sad to say.
danfo
ah, index or value might be a reserved word, you can change them to something else.
halkeye