7

I need to load function to get data from external JS included in HTML file, and I'm doing this:

<body onLoad="getTicket();">
......
</body>

or this:

<html>
<body>
    <head>
    <script src="js/jquery.js" type="text/javascript"></script>
    <script src="js/functions.js" type="text/javascript"></script>
    <script>
        $(document).ready(function() {
            getTicket();
        });
    <script>
    </head>
<body>
</html>

or this:

<html>
<body>
    <head>
    <script src="js/jquery.js" type="text/javascript"></script>
    <script src="js/functions.js" type="text/javascript"></script>
    <script>
        getTicket();
    <script>
    </head>
<body>
</html>

And I have this in functions.JS:

functioOne() {

}

functionTwo() {

}

$(document).ready(function() {
    ...
    .....
    function getTicket() {
        //to do
    }
});

But does'nt work and in the console display this:

Uncaught ReferenceError: getTicket is not defined 

Regards.

3
  • how are you including that external js file into that HTTML file ? Commented Feb 16, 2013 at 8:47
  • What browser are you using? Does it produce errors in your console? If there's a parsing error inside your functions.js file, the browser may 'skip' the rest of the file, causing your getTicket() function to 'not' being defined Commented Feb 16, 2013 at 8:59
  • The head tag has to be put before the body tag - especially when you want <body onLoad="getTicket();"> Commented Apr 20, 2021 at 20:27

5 Answers 5

9

Your getTicket function is defined only in the context (scope) of the jQuery closure (anonymous function). Define it in the global scope instead (elsewhere in the file and not as a "function parameter").

If you need variables from that scope, encapsulate them in a namespace (an object), or declare it as window.getTicket = function() { /* ... */}.

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

Comments

5

Try putting the function getTicket(){} outside or the doc ready:

functio One() {

}

function Two() {

}
function getTicket() {
    //to do
}

$(document).ready(function() {
...
.....
   getTicket();
});

Your order of inclusion is perfect no issues with that.

<script src="js/jquery.js" type="text/javascript"></script>
<script src="js/functions.js" type="text/javascript"></script>

This is perfectly fine.

3 Comments

The problem is that getTicket() contains jQuery code and for that reason I can't put out.
There should not be any issues i think but you can try this though.
@SoldierCorp the document on ready part just makes sure that the DOM is populated before your jQuery runs. You can define code outside of it, just so long as it isn't called yet. (now whether or not you should is another question)
4

you could do this:

$(document).ready(function() {
  ...
  .....
  window.getTicket = function() {
    //to do
  } 
});

once the document is ready, you will be able to call getTicket

1 Comment

You mean, that I put that code in external js or in html file?
0

Add <script type="text/javascript src="external.js"></script> to head.

Then you have to wait for the document to be fully loaded.

The problem is that your code gets executed this way:

  • execute external.js
  • your function is not defined because the document hasn't finished loading
  • you call the undefined function
  • the document is ready

IMHO I don't think is a good idea to define a function inside $(document).ready. It would be simpler to normally define that function, then call it inside $(document).ready.

1 Comment

I -1'd you when your answer was just to "add the script tag" (which had nothing to do with the problem that the scope of the function was a closure, which means that he couldn't ever see it). Now you've edited it so I'm un-minus-oneing-it.
0

You must include the js file before calling the function. If you do This:

<html>
<body>
    <head>
    <!-- include script files here.(jquery and custom script files) -->
    <script>
        $(document).ready(function() {
            getTicket();
        });
    </script>
    </head>
<body>
</html>

you must include your script file before calling the function.

1 Comment

Obviously that I have it, is the first thing you should do.

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.