Jump to content

jquery function scope.


mrbean

Recommended Posts

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?

Link to comment
Share on other sites

<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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :D

 

});

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :D

 

});

 

I don't... what? Why is that better? Why not just define it normally?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.