2

I want to call jquery function in side of java script. My code is:

<script type="text/javascript">
   function calljs(){
      getUserMail(usermMail);
      }

  $(function() {
      function getUserMail(usermMail) {
       ***some code*****
      }
  });
</script>

I got error from browser console:

ReferenceError: getUserMail is not defined.

How to solve this problem?

5
  • 2
    For calljs function, there's no getUserMail function to access. Commented Oct 20, 2014 at 6:10
  • it cannot call a function within another function, from another function. Commented Oct 20, 2014 at 6:13
  • If possible for call jquery function from java script. Commented Oct 20, 2014 at 6:14
  • You can call Jquery yes. Why are you trying to attach this function to Jquery? You basically have your function in an unnamed closure, thereby making it so you cannot access it. Commented Oct 20, 2014 at 6:15
  • I am using Google api for get email so i want to pass gmail id from java script function to jquery. Commented Oct 20, 2014 at 6:22

4 Answers 4

3

As far as i understand, the method is not defined when the method is being called. So define it before it is getting called

<script type="text/javascript">

   function getUserMail(usermMail) {
       ***some code*****
   }

   function calljs(){
      getUserMail(usermMail);
      }

  $(function() {
      // 
  });
</script>

hope it helps

Sign up to request clarification or add additional context in comments.

Comments

1

If it is really compulsory to put the function with in the jquery's ready callback (which I don't think is compulsory) use the following way

<script type="text/javascript">
   var getUserMail = null;
   function calljs(){
      if ( null !== getUserMail ) {
          getUserMail(usermMail);
      }
   }

  $(function() {
      getUserMail = function (usermMail) {
       ***some code*****
      }
  });
</script>

Comments

0

You can simply do ,

 $(document).ready(function(event) {
            getUserMail(usermMail);
        });

and define it like ,

function getUserMail(usermMail){
. . .
}

or using jquery ,

$(document).on('click', ".selector", function);

trigger a function on an event

Comments

0

getUserMail is not defined in a scope that is accessible to calljs. This is why you get the ReferenceError; in the context in which you tried to invoke getUserMail there was no function with that name available.

   // At this point nothing is defined
   function calljs(){
      getUserMail(usermMail);
   }
   // now calljs is defined as a global and can be invoked from anywhere


  $(function() { // this line is calling a function named $ (an alias for jQuery)
                 // and passing it an anonymous function as a parameter.
      function getUserMail(usermMail) { // This function is being defined inside
                                        // the scope of the anonymous function,
                                        // it can be used anywhere inside the 
                                        // anonymous function but not outside it.
       // ***some code*****
      }
  });
  // we are now outside the scope of the anonymous function,
  // getUserMail is no longer in our scope and can't be called from here.

The easiest and likely best solution for most situations would be to make sure that any functions that call each other are in the same scope.

From what I can tell you don't really need calljs, you were just trying to use it to poke a hole into the scope of the anonymous function where getUserMail is defined.

Instead you should probably get rid of calljs and move any code that is calling getUserMail inside the ready callback. If getUserMail needs to wait for the ready callback to be fired before you call it, any code that invokes it also should be inside the ready callback too. (Things like event handlers that call it should already be inside the ready callback anyway.)

If there is a reason that you can't move it into the ready callback, such as something in another .js file needs to be able to call it etc, your application might be too complicated to be realistically maintained as jQuery soup. It might be worth the effort to port it to a framework such as Ember or Angular.

Also so you know, there is no need to use the type attribute on your script tags. JavaScript is the only language that has wide support in the browser and all browsers default to using JavaScript for script tags.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.