> I'm not to sure why the developer
> wrapped it all in the ready() function but I'll have to painfully pull
> it apart now... nothing's ever easy is it :)

Maybe they wanted to try not to pollute the global namespace? Were
there a lot of functions defined?
I guess if that was the idea and you want to do it also, you'd might
consider turning the functions into jQuery plug-ins, or (easier) just
define a single global object variable as a namespace for those
function and define the functions within that object.

Ex:

// define one global object with set of functions
var myFunctions = {
    walk: function(steps) {
        // walk
    },
    jump: function(times) {
        // jump
    }
};

// call functions
myFunctions.walk(10);
myFunctions.jump(5);


On Sep 21, 2:20 pm, Matthew <matthewbchamb...@gmail.com> wrote:
> Hi guys
>
> Thanks for the great feedback. MorningZ was right on the money with my
> scenario. I'm incorporating a js file of a photo gallery which was
> built by some else and all the code is wrapped in the $(document).ready
> () function.
>
> So it looks like you all seem to agree that all the methods inside
> this function are not available globally so I'm going to have to re-
> factor the code in the js file. I'm not to sure why the developer
> wrapped it all in the ready() function but I'll have to painfully pull
> it apart now... nothing's ever easy is it :)
>
> Cheers
> Matthew
>
> On Sep 22, 10:09 am, Mike McNally <emmecin...@gmail.com> wrote:
>
> > Well "Action1" is a local function to that jQuery "ready" function, so
> > it's *never* going to be available as a global function.
>
> > A couple of options:
>
> > 1) Turn your wannabe-global functions into jQuery plugins
>
> > 2) Explicitly add your functions to the window object
> >       window['Action1'] = function() { ... };
>
> > 3) Bind your functions as event handlers on the "body" object (or whatever)
> >       $('body').bind('Action1', function() { ... });
>
> >       ....
>
> >       <body>
> >         <div> <!-- whatever -->
> >             <script> $('body').trigger('Action1'); </script>
>
> > On Mon, Sep 21, 2009 at 7:03 PM, MorningZ <morni...@gmail.com> wrote:
>
> > > Do you have like this:
>
> > > External js file "code.js":
>
> > > $(document).ready(function() {
> > >       function Action1() {
> > >          // do stuff
> > >       }
> > > });
>
> > > Html file "index.htm":
>
> > > <html>
> > >    <head>
> > >       <script type="text/javascript" src="code.js"></script>
> > >    </head>
> > >    <body>
> > >         ... some html here ...
> > >         <script>Action1()</script>
> > >         ... some html here ...
> > >    </body>
> > > </html>
>
> > > if so, then "Action1" isn't defined yet (and it won't be until *all*
> > > the HTML is spit out to the browser) and not available to use.....
>
> > > see this quick example:
>
> > >http://jsbin.com/edovi/edit
>
> > > the alert of "Run Action1" happens *before* the document is ready, and
> > > as a result, the event getting wired up
>
> > > On Sep 21, 7:11 pm, Matthew <matthewbchamb...@gmail.com> wrote:
> > >> Hi all
>
> > >> I've got a js file where all the functions are wrapped inside $
> > >> (document).ready(). I want to call one of the function from within the
> > >> HTML but it says that the function "is not defined". Any ideas?
>
> > >> Cheers
> > >> Matthew
>
> > --
> > Turtle, turtle, on the ground,
> > Pink and shiny, turn around.
>
>

Reply via email to