Search code examples
javascriptjqueryfancybox

Fancybox plugin: rewrite href option on the fly


I have got some links like:

<a href='http://mysite.com/comment/123/edit' class='fancybox'>Edit comment</a>

And Fancybox plugin:

$(".fancybox").fancybox();

When I click on the link I'll see some page from http://mysite.com/comment/123/edit, but I want to see http://mysite.com/comment/123/edit.js.

I know there is href option, but how can I rewrite it to append .js at the end of my original href.


Solution

  • You can change the href attribute as you apply fancybox:

    $(".fancybox").attr('href', function() { return $(this).attr('href') + '.js'; }).fancybox();
    

    If you don't want to change the attribute (keep the link the same), but want it applied for fancybox only, use the href option you mentioned:

    $(".fancybox").each(function () {
        $(this).fancybox({ href: $(this).attr('href') + '.js' });
    });