var say_something; $(document).ready(function(){ say_something = function(word){ alert(word); } });
Karl Rudd On Mon, Apr 28, 2008 at 6:01 AM, Yuval <[EMAIL PROTECTED]> wrote: > > Hey Mike, > Thanks a lot for your explanation! > Say I insisted on defining it within the $(document).ready function, > how would I access a function that is located inside another function? > Thank you, > Yuval > > On Apr 27, 12:37 am, "Michael Geary" <[EMAIL PROTECTED]> wrote: > > Hi Yuval, > > > > It looks like you've solved the problem, and your solution is correct. > > > > The reason you didn't find any jQuery-specific information about this is > > that it isn't a jQuery problem at all. It's a matter of JavaScript scoping. > > > > Your say_something function is defined inside another function, therefore > > the name "say_something" is local to that outer function and not available > > outside it. > > > > You'd have exactly the same problem if settings.js looked like this: > > > > function foobar() { > > function say_something(word){ > > alert(word); > > } > > > > } > > > > There is no need for your say_something() definition to be located inside a > > $(document).ready() callback function. By defining it directly at the top > > level (not inside another function), you make it available globally, which > > is just what you need: > > > > function say_something(word){ > > alert(word); > > > > } > > > > -Mike > > > > > From: Yuval > > > > > Hey Guys, > > > I have 2 files. index.html and settings.js > > > > > inside settings.js: > > > > > $(document).ready(function(){ > > > function say_something(word){ > > > alert(word); > > > } > > > }) > > > > > inside index.html > > > > > <script language="javascript"> > > > $(document).ready(function(){ > > > say_something("hello"); > > > }); > > > </script> > > > > > I am using Firefox 2 with Firebug and it claims the function > > > is not defined. The code does not work. > > > So why is this a jQuery problem, you ask? > > > Well, if I remove the $(document).ready call from the > > > external file, everything works fine! > > > > > this is what it looks like when it works: > > > > > inside settings.js: > > > > > // no $(document).ready call > > > function say_something(word){ > > > alert(word); > > > } > > > > > inside index.html > > > > > <script language="javascript"> > > > $(document).ready(function(){ > > > say_something("hello"); > > > }); > > > </script> > > > > > I've been searching Google and the forum for almost 2 hours > > > now with no answer to why this does not work with > > > $(document).ready. Any ideas? > > > Thank you, > > > Yuval > > > > > p.s. replacing <script language="javascript"> with <script > > > language="text/javascript"> simply suppresses the error, it > > > does not solve the problem. >