Kevin Szmyd
Kevin Szmyd

Reputation: 11

How can I call back a function?

I'm a bit new to the jquery codeverse. I would like to know how to call back function CSZ after a document resize. Here is my current setup:

$(document).ready(function CSZ () {|CODE|});

Then later, I want to call the code back with this function:

$(window).resize(CSZ);

It seems to be a very simple concept that just isn't working for me.

Upvotes: 1

Views: 78

Answers (4)

Ja͢ck
Ja͢ck

Reputation: 173642

In your current code the symbol CSZ will only be accessible from inside the function body itself; it's actually a language feature.

In order for this to work as expected, your function needs to be declared like this, on its own and in the global scope:

function CSZ () {|CODE|}

And then it can be used like this:

$(document).ready(CSZ);

$(window).resize(CSZ);

Upvotes: 1

James Allardice
James Allardice

Reputation: 166021

Declare the function on its own. You can then reference it for both callbacks:

function CSZ() {
    // Do stuff
}
$(document).ready(CSZ);
$(window).resize(CSZ);

Currently, CSZ is a named function expression. The identifier CSZ will only be in scope inside the function it identifies. By changing to to follow my example, you make CSZ a function declaration instead. It will then be available anywhere within the scope in which it appears (and descendant scopes thereof).

Upvotes: 4

Denys Séguret
Denys Séguret

Reputation: 382274

Your problem is that the function you declared isn't declared as a window variable.

You could do this :

$(document).ready(window.CSZ=function(){...});
$(window).resize(CSZ);

Or you could follow this more common pattern :

$(function(){
    function CSZ(){
       ...
    };
    CSZ();
    $(window).resize(CSZ);
});

Upvotes: 0

yunzen
yunzen

Reputation: 33439

function CSZ () {|CODE|}

$(document).ready(CSZ)

$(window).resize(CSZ);

Upvotes: 0

Related Questions