First I am using the jQuery colorbox plugin that is working fine so far but then I want to close the colorbox using a button. Unfortunately I can't just trigger the click event on that button by selecting its ID using jQuery, instead of that the button must call a javascript function called closepan() (this behavior is unfortunately mandatory for me).

I tried to create the function

```
closepan() {
$.colorbox.close();
}

```

**first case** : inside the

```
$(document).ready(function(){...}); 

```

but then I got the error closepan is undefined.

**second case** : before the 

```
$(document).ready(function(){...}); 

```

but then it's the colorbox method that is undefined!

I gave up after gazillion hours of fiddling with several solutions I've found all around stackoverflow.com regarding this topic! I can't figure out how to make this working!

In other words, how to create a function named closepan() that can execute $.colorbox.close(); while being available globally for my button?
['https://Stackoverflow.com/questions/9952128', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1207999/']
No matter where you create a variable or function if you create it on `window` it will be available globally.

```
window.closepan = function() {
   // hello there
}

```
```
function closepan() {
    if($.colorbox) {
        $.colorbox.close();
    }
}

```

However, at the point where someone clicks your button all external scripts should have been loaded so that check shouldn't be necessary...