Friday, June 26, 2009

[jQuery] Re: call a method outside a jquery object

Hi Eric,

Sorry I said a lot of nonsense before (and maybe I am still doing it)... so please don't consider my previous mail.

one function [SlideMenu.js] contains several methods to animate and manage a slide menu.
I would like to open the slide menu when a particular condition it happens. (1 time an user arrive to my web site)

so in order of doing that, I use the cookiePlugin inside my readyFunction [init.js]

There is also a condition (if statement) to check if a user has been on my web site and as a result of that i also would like to invoke the call contained in that function


[init.js ] // JavaScript Document $(document).ready(function(){ 	  	checkPreferences();           function checkPreferences(){ 		if ($.cookie('language')!=null){ 			//do something 		}else{ 			//put here instructions that need to be executed the first time  		}if ($.cookie('skin')!=null){ 			//change skin 		}else{ 			//put here instructions that need to be executed the first time 			openSlideMenu();// that method is inside [myFunction.js ] below 		} 	 	}  });  -----------------------------------------------------  [SlideMenu.js ] // JavaScript Document   $(         function(){                 var x = "Hello world";                 function openSlideMenu(){                         //open the menu                 }         } );

What do you think?


Please, I am very interested to the long short story, where can I read it?
Cheers





jakiri hutimora ha scritto:
Thanks Eric,

Actually I really need to access to a method inside another function.
I created a simple example however my aim is calling a "public method" (let me use public) that has been defined inside a function like that

$(         function(){                 function openMenu(){                         doSomething(x);                 }                 function closeMenu(){                         doSomething(x);                 }          } );   
the real method open a slide menu, in fact all the function define several method to manage this menu and I would like that openMenu is called when the page has been loaded.

For that reason, because I have already a function that contain a $(document).ready(function()
I simply wanted to nest the call to my "public method" inside that function.

Are you suggesting to add a $(document).ready(function() inside my slideMenu Function?

like that

$(         function(){ 		$(document).ready(function(){openMenu(); 		});                 function openMenu(){                         doSomething(x);                 }                 function closeMenu(){                         doSomething(x);                 }          } );

Please, I am very interested to the long short story, where can I read it?
Cheers



Eric Garside ha scritto:
Long story short, you can't do what you're trying to do. You have some massive scoping issues. first, anything within you document.ready function (as defined by $(funciton(){..});) that gets declared in there is accessible only inside that function itself.  Now, I'm not sure what you're actually trying to accomplish here, as this example is a bit on the simple side. Could you give a broader idea of your goal? That'd help in putting you on the right direction to the best way to get this done (as if you're merely trying to echo a variable, there's way better implementations than what you've described).  On Jun 26, 5:06 am, jasper saronno <jakirihutim...@gmail.com> wrote:   
Hello Everybody,  I am not sure I have explained well enought what I mean.  I found very difficult to call a method outside his scope.  For example I have 2 files js  myFunction.js  init.js (where I initialize all my page)  [init.js ] // JavaScript Document $(document).ready(function(){         //call getX();  });  -----------------------------------------------------  [myFunction.js ] // JavaScript Document   $(         function(){                 var x = "Hello world";                 function getX(){                         alert(x);                 }         } );  I tried many syntax in order to call the method outside its scope  $.getX();  $(function(){getX()});  $().function().getX();  $("myHtmlObj").click(function(){getX()});  I really would like to understand that issue, if you also may suggest the title of a good book and the chapter that explain it I would really appreciate.  Many thanks - jasper     
   

No comments: