mrbean Posted August 14, 2012 Share Posted August 14, 2012 My code is like this: $(document).ready(function() { function test(){} }); Out of the ready function I want to execute test(), so in another js file: test(); But if I run the code, I get an error that test(); is not defined. How to solve this? Quote Link to comment https://forums.phpfreaks.com/topic/267070-jquery-function-scope/ Share on other sites More sharing options...
Jessica Posted August 14, 2012 Share Posted August 14, 2012 Save the code as an external file, and then include it on both pages. You should not run jquery code outside of the document ready function. Quote Link to comment https://forums.phpfreaks.com/topic/267070-jquery-function-scope/#findComment-1369361 Share on other sites More sharing options...
mrbean Posted August 14, 2012 Author Share Posted August 14, 2012 Save the code as an external file, and then include it on both pages. You should not run jquery code outside of the document ready function. How can I include that? Do you have an example? Quote Link to comment https://forums.phpfreaks.com/topic/267070-jquery-function-scope/#findComment-1369376 Share on other sites More sharing options...
Jessica Posted August 14, 2012 Share Posted August 14, 2012 Include it the same way you do the jquery library. <script type="text/javascript" src="file.js"></script> or something. Quote Link to comment https://forums.phpfreaks.com/topic/267070-jquery-function-scope/#findComment-1369378 Share on other sites More sharing options...
Christian F. Posted August 14, 2012 Share Posted August 14, 2012 <script></script>.... Are you actively trying to get people to ignore you, or what? We're not here to do the work for you, you know. We've got plenty of other, and better, stuff to do than to spend our free time doing the basic research for you. If you can't be arsed to spend a little bit of time researching the bascis, why should we be arsed to spend our time doing it for you? Read ManicDan's signature. Quote Link to comment https://forums.phpfreaks.com/topic/267070-jquery-function-scope/#findComment-1369379 Share on other sites More sharing options...
Adam Posted August 14, 2012 Share Posted August 14, 2012 To answer your original question mrbean, the code within the document ready event is wrapped in an "anonymous" function. .ready(function() { ... }); Essentially it's just a nameless function, but you're still within the context of a function. In JS you can define 'private' functions within functions, but they're limited to the parent function's scope. So when you define test() within document event, you're restricting it to just that anonymous function. If you were to define it out of the document ready event and in the global scope, it would be accessible anywhere. @jesirose Why shouldn't you use jQuery outside of the document ready event? There's good and bad ways of doing it of course, but if you're using jQuery as your JS framework then it's pretty unavoidable that you're going to need it somewhere else. @ChristianF Calm down man. The OP came here asking about function scope, I'm sure he just got a little confused with the response about including stuff. Quote Link to comment https://forums.phpfreaks.com/topic/267070-jquery-function-scope/#findComment-1369400 Share on other sites More sharing options...
Christian F. Posted August 14, 2012 Share Posted August 14, 2012 Adam: I'm quite calm, thank you. No point in getting riled up over other people's actions, after all. Though, having just read another thread of his, well... I think you'll understand why I was wondering? Quote Link to comment https://forums.phpfreaks.com/topic/267070-jquery-function-scope/#findComment-1369401 Share on other sites More sharing options...
Jessica Posted August 14, 2012 Share Posted August 14, 2012 Because if you try to execute javascript before the document is ready, you can and likely will run into problems. I have used jQuery for years and NEVER needed to run jQuery code outside of the document ready block. Show me an example of code where you needed to. I'm not talking about defining a function, I'm talking about executing one, like the OP is trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/267070-jquery-function-scope/#findComment-1369402 Share on other sites More sharing options...
mrbean Posted August 14, 2012 Author Share Posted August 14, 2012 <script></script>.... Are you actively trying to get people to ignore you, or what? We're not here to do the work for you, you know. We've got plenty of other, and better, stuff to do than to spend our free time doing the basic research for you. If you can't be arsed to spend a little bit of time researching the bascis, why should we be arsed to spend our time doing it for you? Read ManicDan's signature. Could you please please be more friendly. And sorry for my english, I am not an expert in it. Like Adam said, I thought there is an function to import an external file. Just like java. example: import my.package.MyClass; @jesirose, It doesn't work. Why? Just like Adam said: ".....So when you define test() within document event, you're restricting it to just that anonymous function." And everytime I start a new: $(document).ready(function(){ }); It will create a new unique anonymous function. Those functions created in the anonymous function will be only available in the anonymous function itself. For example, this won't work: Index.html <html> <head> <script language="JavaScript" src="first.js"> </script> <script language="JavaScript" src="second.js"> </script> </head> <body> Just some content. </body> </html> First.js $(document).ready(function(){ function test(){ alert("This is a test"); } }); Second.js $(document).ready(function(){ test(); }); ERROR: start is not defined. I can define it outside of document ready, but I want to work within the document ready function. Just to wrap the code and be more organized. Sorry for my english. Quote Link to comment https://forums.phpfreaks.com/topic/267070-jquery-function-scope/#findComment-1369432 Share on other sites More sharing options...
Adam Posted August 14, 2012 Share Posted August 14, 2012 Because if you try to execute javascript before the document is ready, you can and likely will run into problems You will only run into problems if you try to access DOM elements that don't exist yet. It's pretty common to put your "main" JS file within the foot of the document though, and just execute the code there and then (sometimes with a closure around jQuery), outside of any ready event. I have used jQuery for years and NEVER needed to run jQuery code outside of the document ready block. Show me an example of code where you needed to. I'm not talking about defining a function, I'm talking about executing one, like the OP is trying to do. I'm not saying you shouldn't put your code within the document ready event, I'm saying it's not a requirement, so you shouldn't tell people it is. An by using it somewhere else I was thinking more in terms of modular JS, or general objects where you pass in jQuery as a dependency. ---- I can define it outside of document ready, but I want to work within the document ready function. Just to wrap the code and be more organized. Lumping everything into a document ready event is not organised, it's just going to get messy and a pain to maintain if you write enough code. If you really want to do it right, take a look at RequireJS and modular JS that I alluded to above. Quote Link to comment https://forums.phpfreaks.com/topic/267070-jquery-function-scope/#findComment-1369436 Share on other sites More sharing options...
Jessica Posted August 14, 2012 Share Posted August 14, 2012 There is nothing in your code that has anything to do with "start". You CAN define it outside of doc ready. You have to. Then you INCLUDE that on each page. Then in your document ready you CALL it. Read what I said. "You should not run jquery code outside of the document ready function." You SHOULD NOT RUN jQuery code... Should not. Run. Not "cannot define". Not "will burst into flames if you define". Geez. And no, people aren't going to be friendly when you ask questions that can be answered by google. Quote Link to comment https://forums.phpfreaks.com/topic/267070-jquery-function-scope/#findComment-1369437 Share on other sites More sharing options...
Jessica Posted August 14, 2012 Share Posted August 14, 2012 Where did I say it was a requirement?? See above. Quote Link to comment https://forums.phpfreaks.com/topic/267070-jquery-function-scope/#findComment-1369439 Share on other sites More sharing options...
Adam Posted August 14, 2012 Share Posted August 14, 2012 Where did I say it was a requirement?? See above. I'm not on about the function definitions.. Quote Link to comment https://forums.phpfreaks.com/topic/267070-jquery-function-scope/#findComment-1369440 Share on other sites More sharing options...
mrbean Posted August 14, 2012 Author Share Posted August 14, 2012 Where did I say it was a requirement?? See above. I'm not on about the function definitions.. Thanks for your replies Adam. Found it! $(document).ready(function() { test=function(){ alert(123); }; // global var }); Quote Link to comment https://forums.phpfreaks.com/topic/267070-jquery-function-scope/#findComment-1369441 Share on other sites More sharing options...
Jessica Posted August 14, 2012 Share Posted August 14, 2012 Where did I say it was a requirement?? See above. I'm not on about the function definitions.. Then what are you talking about? I said you should not run jquery code outside of the doc ready. If you guys both interpreted that wrong fine, but that's what I said. Quote Link to comment https://forums.phpfreaks.com/topic/267070-jquery-function-scope/#findComment-1369445 Share on other sites More sharing options...
Adam Posted August 14, 2012 Share Posted August 14, 2012 I'm saying, you don't need to put any jQuery code in document ready. Unlike what you said: You should not run jquery code outside of the document ready function. Nor am I saying it's a bad thing to do.. Let's just let the argument rest ---- Found it! $(document).ready(function() { test=function(){ alert(123); }; // global var }); I don't... what? Why is that better? Why not just define it normally? Quote Link to comment https://forums.phpfreaks.com/topic/267070-jquery-function-scope/#findComment-1369453 Share on other sites More sharing options...
mrbean Posted August 14, 2012 Author Share Posted August 14, 2012 Where did I say it was a requirement?? See above. I'm not on about the function definitions.. Then what are you talking about? I said you should not run jquery code outside of the doc ready. If you guys both interpreted that wrong fine, but that's what I said. I understand you should not do that. If you are talking to me? Quote Link to comment https://forums.phpfreaks.com/topic/267070-jquery-function-scope/#findComment-1369454 Share on other sites More sharing options...
Jessica Posted August 15, 2012 Share Posted August 15, 2012 I agree the OP's solution is way off. That is pretty much the opposite of what we're both saying to do. Quote Link to comment https://forums.phpfreaks.com/topic/267070-jquery-function-scope/#findComment-1369569 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.