Call Colorbox in Javascript

Amendment : Please read this post for additional details.

Recently someone used the above to get to my site and I realize I haven’t written a post yet to explain what you can do about calling the Colorbox. After much trial and error on a project at work I ended up with a few options.

For those that don’t know how Colorbox is called…

Firstly you have an element with a class or id, i’d probably use an id unless you have a class that is pretty unique for the page (like the name of a button) for this case, let’s name the id as showGoogle

$(“#showGoogle”).colorbox({href:”http://www.google.ca/”});

The above will open a colorbox with Google’s homepage on it. Now the issue that came up afterwards is how do you do things inside the colorbox or access things in the colorbox once it’s up. Well, first, let’s show you two other things you can do.

Before you open your colorbox (I use this at work because we need to hide our videos before the colorbox opens):
$.bind(‘cbox_open’, function(){alert(“Whatever you want!”);});

After your colorbox is closed (I used it to bring back the videos):
$.bind(‘cbox_closed’, function(){alert(“Whatever you want when it’s closed!”);});

And finally there’s this other bit that works once it’s loaded:
$(“#showGoogle”).colorbox({href:”http://www.google.ca”}, function(){alert(“Show this bit here!”)});

The last issue I ran into was how do I get Javascript to go off on things inside the colorbox? I called it inside the code that shows up. Calling the function on the side with $(“#whatever”).click(function(){}); didn’t work, so I put the things I wanted in the click into a function, then called that function in the onClick.

So far I haven’t found an easier way about it, but I’m sure I’ll find it soon and I’ll update this post in the future to make sure it’s up to date.

Also someone searched for a colorbox bug in chrome, what was the bug? Maybe I can post the solution here or another colorbox post.

Leave a comment

6 Comments.

  1. Jesse Leite

    Awesome, I spent 2 hours looking for this. I am new to javascript and so this really helped me hide and show my video elements with colorbox. Thank you so much for the post; very clear and concise!!! -Jesse

  2. A.R. Van Halleorg

    No problem at all :) Like I said it took me quite a few tries and errors to get the dang thing to work. I’m just glad it helped someone.

  3. definitely helped. i’m using it here: http://www.resourcestage.ca/newnew5/
    there’s a youtube video set to autoplay, so whenever colorbox opens it takes the HTML embed code for the video right out, then puts it back in on colorbox close. exactly what i needed, thank you again.

  4. A.R. Van Halleorg

    Oh my, and your page just let me know i could get the embed inline to show in the colorbox properly. Thanks for the help back :)

  5. Jesse Leite

    Not sure what you mean?

  6. A.R. Van Halleorg

    I had tried to get an embed to show in the colorbox, but it only worked when I threw it to a called page, I thought it was a bug somewhere. Now I can play around with it knowing it _does_ work.

Leave a Reply


[ Ctrl + Enter ]