4

I've seen several issues about issues related to this, but none that provided a definite answer.

In this JavaScript:

function global_function() {
    a_jquery_function();
}

$(function () {
    var a_jquery_function = function () {
        do_something();
    };
});

Can I do something so that the global function can call the jQuery function? Obviously, it can't be done the way I do it there, but I was wondering if it could be done using jQuery.fn or using custom events. For example:

function global_function() {
    $.fn.a_jquery_function();
}

$(function () {
    $.fn.a_jquery_function() = function () {
        do_something();
    };
});

or

function global_function() {
    trigger_event('my_event', args);
}

$(function () {
    $(document).on('my_event', function(args) {
        do_something();
    });
});

I've tried the first one, but I ran into problems. If I know either is possible, I'll work on them more. But if someone out there knows that it's not possible to do this, I suppose I'll have to give it up.

Thanks.

8
  • What's the actual case this is needed for? Commented Dec 18, 2013 at 20:59
  • Thanks for the edit, tymeJV. I proofread the post, but obviously not the title. Oops. Commented Dec 18, 2013 at 20:59
  • Why would it be not possible to do either? Do you suspect that there are unyielding laws of programming physics that will prevent it? The first instance is really just creating a global variable since you are attaching a property to an object reachable from the global variable $. Commented Dec 18, 2013 at 21:00
  • Why do you place brackets at the end of variable's name? Commented Dec 18, 2013 at 21:02
  • I need to use the YouTube IFrame API which requires functions to be declared globally. The main one is the event handler for when the video frame is ready. But I want these function to be able to interact with the rest of the code, which is defined in the jQuery block. I also don't want more than the absolute minimum amount of functions to be defined in the global scope. Commented Dec 18, 2013 at 21:02

4 Answers 4

12

You can attach the functions to the jQuery namespace, in a sense turning them into jQuery plugins. at that point, they will be available wherever jquery is.

function global_function() {
    $.a_jquery_function();
}

$(function () {
    $.a_jquery_function = function () {
        do_something();
    };
});
Sign up to request clarification or add additional context in comments.

2 Comments

There's one possible issue though. What if global_function gets executed before dom ready? you would be better off defining the function outside of dom ready. This possible issue would affect both of our code samples.
I'm aware of the issue, but in this case, it's not possible. Your answer looks ridiculously obvious to me now, but was thinking of it from another angle. I'm adding ONE more element to $ and everything else is inside of the jQuery scope. It's absolutely perfect. Thanks again.
8

If you don't want to pollute global namespace, create object and append functions to it.

var myModule = {};
$(function(){
    var foo = function(){/*code*/};
    myModule.bar = foo;
});

Then you call your function from anywhere by using myModule.bar();

4 Comments

Wouldn't the myModule object fall into the global namespace since your declaring it outside of any other object or function?
No, myModule would be local to the current script. It's pretty much what I was looking for. I'll try it.
@eje211 The current script should be in the global scope of the window (i.e. the object falls into the global namespace).
No, myModule would be global. But it's only one object, and author said "I don't want more than the absolute minimum amount of functions to be defined in the global scope". It's absolute minimum - one object in global namespace.
3

instead of using var functionname=function(){} you should use window.functionname=function{} when declaring the function you want available globally. All global functions are properties of the window object, but the reverse is also true, all properties of the window object are global functions.

3 Comments

The basics is that a function creates a new scope, anything declared within the scope using the var keyword will be local to the scope unless explicitly shared somehow. Using window.functionname is one way, returning the function pointer would be another and there are many more. But once you understand scope, this question will be easy to answer.
That would work, but the point here is not to make a global function. window.functionname is, in the context of DOM programming global. It works, but I was hoping for something tidier than that.
@eje211 using window.functionname is simply a way of exporting the method, replacing 'window' with any property that is available globally would work just as easily.
0

you can write your global jQuery function like this

(function($){
  $.fn.func_name = function(var){
      write your code and functions here
  }
})(jQuery);

in your html or js file call $().func_name(var) to register.


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.