Search code examples
javascriptjqueryajaxcolorbox

colorbox sending to the wrong url?


I have multiple colorbox in one page.

those colorbox's links are generated with javascript.

Now every time a new link that opens a colorbox get generated any clicked link associated with colorbox will go to that new link's url.

this is how i declared the colorbox:

$('.ajax').colorbox();

the href in the link are correct but still send to the last generated link's href.

Thanks in advance.

Edit : this is the code how i add some links:

$('.albums').live('click', function(e){
    e.preventDefault();
    e.stopPropagation();
    parent_id           = $(this).parents().eq(2)[0].id;
    parent_header       = $('#' + parent_id + ' header')[0].innerHTML;
    parent_section      = $('#' + parent_id + ' section')[0].innerHTML;
    parent_footer       = $('#' + parent_id + ' footer')[0].innerHTML;
    fragment            = $(this).attr('href').split('/')[$(this).attr('href').split('/').length - 1];
    parent              = $(this).parents().eq(2)[0].id;
    if(fragment.indexOf('_-_') !== -1)
        page = fragment.replace('_-_', '/');
    else
        page = fragment;
    old_data[parent_id] = {'header': parent_header, 'footer': parent_footer, 'section': parent_section, 'link': parent + '/' + page};
    $.post('files/get_page', { page: old_data[parent_id]['link'] }, function(data){
        ret     = JSON.parse(data);
        a       = $('<a>').addClass('reset_link').attr('href', '#').append('Back To Albums');
        a.click(function(e){
            e.preventDefault();
            e.stopPropagation();
            this_parent_id      = $(this).parents().eq(1)[0].id;
            $('#' + this_parent_id + ' header').html(old_data[this_parent_id]['header']);
            $('#' + this_parent_id + ' section').html(old_data[this_parent_id]['section']);
            $('#' + this_parent_id + ' footer').html(old_data[this_parent_id]['footer']);
        })
        $('#' + parent).children().eq(0).html(a);
        $('#' + parent).children().eq(0).append(ret.header);
        $('#' + parent).children().eq(1).html('');
        for(i in ret.body.images){
            href            = ret.body.href + ret.body.images[i].image_name;
            image_id        = ret.body.images[i].image_id;
            delete_div_id   = 'delete_image_' + image_id;
            body            = $('<div>').addClass('albums_div');
            delete_div      = $('<div>').addClass('delete').append('&#10006; Supprimer').attr('id', image_id);
            a               = $('<a>').addClass('lightbox').attr('href', href);
            a.attr('onclick', 'return false;');
            a.append('&#9673; ' + ret.body.images[i].image_name);
            delete_div.click(function(){
                id          = delete_div_id;
                id_array    = id.split('_');
                $.post('files/delete', { id: image_id, del: id_array[1] }, function(data){
                    $(body).remove();
                });
            });
            body.append(a);
            body.append(delete_div);
            $('#' + parent).children().eq(1).append(body);
            $('.lightbox').colorbox({rel: 'group1'});
        }

        footer  = $('<a>').addClass(ret.footer['attr'].class).attr('id', ret.footer['attr'].id).attr('href', ret.footer['href']);
        footer.append(ret.footer['text']);
        footer.click(function(e){
            e.preventDefault();
            $.colorbox({href: ret.footer['href']});
        });
        $('#' + parent).children().eq(2).html(footer);
    });
});

NOTE: the code generates the right links with the correct href but the problem in the colorbox it self. and I knew this because i did this:

i assigned a live click event to the links with the colorbox like this:

$(".upload_file").live('click', function(){
    console.log($(this)[0].href);
});

and the href printed to the console's the right one but the page that was displayed in the colorbox is wrong.

Edit 2 adding the HTML:

the page before the javascript changing the code:

<section id="reference">
    <header class="inner_header"><h3>R&#201;F&#201;RENCE PAGE SECTION</h3></header>
    <section>
        <h4>Choose an album :</h4>
        <div class="albums_div">
            <a href="res_yass" onClick="return false;" class="albums">&#9733; R&#233;sidence YASSINE</a>
            <div class="delete" id="delete_album_1">&#10006; Supprimer</div>
        </div>
        <div class="albums_div">
            <a href="res_nesr" onClick="return false;" class="albums">&#9733; R&#233;sidence NESRINE</a>
            <div class="delete" id="delete_album_2">&#10006; Supprimer</div>
        </div>
        <div class="albums_div">
            <a href="azerty" onClick="return false;" class="albums">&#9733; Azerty</a>
            <div class="delete" id="delete_album_8">&#10006; Supprimer</div>
        </div>
    </section>
    <footer class="inner_footer"><a href="files/add/reference" class="add_album ajax" id="reference">Add an album</a></footer>
</section>

the page after javascript change the code:

<section id="reference">
    <header class="inner_header"><a class="reset_link" href="#">Back To Albums</a><h4>Azerty</h4></header>
    <section>
        <div class="albums_div">
            <a class="lightbox cboxElement" href="f352702127.jpg" onclick="return false;">◉ f352702127.jpg</a>
            <div class="delete" id="128">✖ Supprimer</div>
        </div>
        <div class="albums_div">
            <a class="lightbox cboxElement" href="f65564287.jpg" onclick="return false;">◉ f65564287.jpg</a>
            <div class="delete" id="129">✖ Supprimer</div>
        </div>
        <div class="albums_div">
            <a class="lightbox cboxElement" href="f184021055.jpg" onclick="return false;">◉ f184021055.jpg</a>
            <div class="delete" id="130">✖ Supprimer</div>
        </div>
    </section>
    <footer class="inner_footer"><a class="upload_file ajax" id="azerty" href="uploader/get_form/reference/azerty">Upload une image</a></footer>
</section>

Solution

  • solved, the problem wasn't with the colorbox i just chenge the code from this:

    $('.albums').live('click', function(e){
        e.preventDefault();
        e.stopPropagation();
        parent_id           = $(this).parents().eq(2)[0].id;
        parent_header       = $('#' + parent_id + ' header')[0].innerHTML;
        parent_section      = $('#' + parent_id + ' section')[0].innerHTML;
        parent_footer       = $('#' + parent_id + ' footer')[0].innerHTML;
        fragment            = $(this).attr('href').split('/')[$(this).attr('href').split('/').length - 1];
        parent              = $(this).parents().eq(2)[0].id;
        if(fragment.indexOf('_-_') !== -1)
            page = fragment.replace('_-_', '/');
        else
            page = fragment;
        old_data[parent_id] = {'header': parent_header, 'footer': parent_footer, 'section': parent_section, 'link': parent + '/' + page};
        $.post('files/get_page', { page: old_data[parent_id]['link'] }, function(data){
            ret     = JSON.parse(data);
            a       = $('<a>').addClass('reset_link').attr('href', '#').append('Back To Albums');
            a.click(function(e){
                e.preventDefault();
                e.stopPropagation();
                this_parent_id      = $(this).parents().eq(1)[0].id;
                $('#' + this_parent_id + ' header').html(old_data[this_parent_id]['header']);
                $('#' + this_parent_id + ' section').html(old_data[this_parent_id]['section']);
                $('#' + this_parent_id + ' footer').html(old_data[this_parent_id]['footer']);
            })
            $('#' + parent).children().eq(0).html(a);
            $('#' + parent).children().eq(0).append(ret.header);
            $('#' + parent).children().eq(1).html('');
            for(i in ret.body.images){
                href            = ret.body.href + ret.body.images[i].image_name;
                image_id        = ret.body.images[i].image_id;
                delete_div_id   = 'delete_image_' + image_id;
                body            = $('<div>').addClass('albums_div');
                delete_div      = $('<div>').addClass('delete').append('&#10006; Supprimer').attr('id', image_id);
                a               = $('<a>').addClass('lightbox').attr('href', href);
                a.attr('onclick', 'return false;');
                a.append('&#9673; ' + ret.body.images[i].image_name);
                delete_div.click(function(){
                    id          = delete_div_id;
                    id_array    = id.split('_');
                    $.post('files/delete', { id: image_id, del: id_array[1] }, function(data){
                        $(body).remove();
                    });
                });
                body.append(a);
                body.append(delete_div);
                $('#' + parent).children().eq(1).append(body);
                $('.lightbox').colorbox({rel: 'group1'});
            }
    
            footer  = $('<a>').addClass(ret.footer['attr'].class).attr('id', ret.footer['attr'].id).attr('href', ret.footer['href']);
            footer.append(ret.footer['text']);
            footer.click(function(e){
                e.preventDefault();
                e.stopPropagation();
                $.colorbox({href: ret.footer['href']});
            });
            $('#' + parent).children().eq(2).html(footer);
        });
    });
    

    to this:

    $('.albums').live('click', function(e){
        e.preventDefault();
        e.stopPropagation();
        parent_id            = $(this).parents().eq(2)[0].id;
        parent_header        = $('#' + parent_id + ' header')[0].innerHTML;
        parent_section       = $('#' + parent_id + ' section')[0].innerHTML;
        parent_footer        = $('#' + parent_id + ' footer')[0].innerHTML;
        fragment             = $(this).attr('href').split('/')[$(this).attr('href').split('/').length - 1];
        parent               = $(this).parents().eq(2)[0].id;
        if(fragment.indexOf('_-_') !== -1)
            page = fragment.replace('_-_', '/');
        else
            page = fragment;
        old_data[parent_id]   = {'header': parent_header, 'footer': parent_footer, 'section': parent_section, 'link': parent + '/' + page};
        $.post('files/get_page', { page: old_data[parent_id]['link'] }, function(data){
            ret = JSON.parse(data);
            swap_content(ret);
        });
    });
    function swap_content(ret){
        a      = $('<a>').addClass('reset_link').attr('href', '#').append('Back To Albums');
        a.click(function(e){
            e.preventDefault();
            e.stopPropagation();
            this_parent_id = $(this).parents().eq(1)[0].id;
            $('#' + this_parent_id + ' header').html(old_data[this_parent_id]['header']);
            $('#' + this_parent_id + ' section').html(old_data[this_parent_id]['section']);
            $('#' + this_parent_id + ' footer').html(old_data[this_parent_id]['footer']);
        })
        $('#' + parent).children().eq(0).html(a);
        $('#' + parent).children().eq(0).append(ret.header);
        $('#' + parent).children().eq(1).html('');
        for(i in ret.body.images){
            href            = ret.body.href + ret.body.images[i].image_name;
            image_id        = ret.body.images[i].image_id;
            delete_div_id   = 'delete_image_' + image_id;
            body            = $('<div>').addClass('albums_div');
            delete_div      = $('<div>').addClass('delete').append('&#10006; Supprimer').attr('id', image_id);
            a               = $('<a>').addClass('lightbox').attr('href', href);
            a.attr('onclick', 'return false;');
            a.append('&#9673; ' + ret.body.images[i].image_name);
            delete_div.click(function(){
                id          = delete_div_id;
                id_array    = id.split('_');
                $.post('files/delete', { id: image_id, del: id_array[1] }, function(data){
                    $(body).remove();
                });
            });
            body.append(a);
            body.append(delete_div);
            $('#' + parent).children().eq(1).append(body);
            $('.lightbox').colorbox({rel: 'group1'});
        }
    
        footer = $('<a>').addClass(ret.footer['attr'].class).attr('id', ret.footer['attr'].id).attr('href', ret.footer['href']);
        footer.append(ret.footer['text']);
        footer.click(function(e){
            e.preventDefault();
            e.stopPropagation();
            $.colorbox({href: ret.footer['href']});
        });
        $('#' + parent).children().eq(2).html(footer);
    }
    

    Explanation : the first code i had the creation of the elements happens globally so every time the code executes it overwrite the variables and i'm assigning the the colorbox to the click function here:

    footer.click(function(e){
        e.preventDefault();
        e.stopPropagation();
        $.colorbox({href: ret.footer['href']});
    });
    

    so every time the footer variable gets rewritten over that click gets reassigned to the new footer that's why colorbox sends the data to the wrong url so all i have to do is to make those variables local to a function in this case the swap_content() function now each footer is unique and therefore colorbox will send the data to the right url. thanks for those who tried to answer. hope this helps anyone.