External JS Call Problem??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    External JS Call Problem??

    I'm having an issue with calling a function from a external linked JS file. The file is linked like so in the header of the page:
    Code:
    <script type="text/javascript" src="file.js"></script>
    And the function that exists in the file.js is called from the body of the html page like so but its not working???
    Code:
    <script>myfunction('test');</script>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    try
    Code:
    <script type="text/javascript">myfunction('test');</script>
    if that fails too, have a look at the error console.

    Comment

    • ziycon
      Contributor
      • Sep 2008
      • 384

      #3
      That didn't work, the error console is saying:
      myfunction is not defined
      But it is in the JS file!?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        did you check writing (JS is case sensitive, so myfunction() and myFunction() are different)?

        Comment

        • ziycon
          Contributor
          • Sep 2008
          • 384

          #5
          Yup, checked this and its all ok, its a stange one as its the way i've always called functions from an external JS file!?!

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            can you post a link to that file?

            Comment

            • ziycon
              Contributor
              • Sep 2008
              • 384

              #7
              This is all thats in the file:
              Code:
              function myfunction(input) {
              	document.write(input);
              }

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                are you sure the file is loaded correctly?

                Comment

                • dmjpro
                  Top Contributor
                  • Jan 2007
                  • 2476

                  #9
                  Originally posted by Dormilich
                  are you sure the file is loaded correctly?
                  Ahh! That's the point ;)
                  Actually what i think that when he calls the "function" then the JS file not loaded.
                  You better to keep your "this type of function(what you call inside HTML body)" in your script tag.

                  Code:
                  <script>
                  //some function definitions
                  </script>
                  ....
                  ....
                  ....
                  ....
                  <script>/*call the function*/</script>
                  ....
                  ....

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    Originally posted by dmjpro
                    You better to keep your "this type of function(what you call inside HTML body)" in your script tag.
                    this sure solves the problem, but you pay it with bandwidth traffic and download time (this is no problem when having only some very small functions, but if you need the benefits of larger libraries, this is not an option)

                    some background:
                    external JS files are treated similar to images, if the browser has this file cached (i.e. in its memory), it will only look if the server provides a newer file and only in that case the file is loaded again (relevant when you need a script on many pages)

                    Comment

                    • dmjpro
                      Top Contributor
                      • Jan 2007
                      • 2476

                      #11
                      Then first time it will not work.
                      And how can i make sure when i make this call then my external file gets loaded completely? ;)

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        Originally posted by dmjpro
                        Then first time it will not work.
                        if you have access to the HTTP request (e.g. in FireBug*) you'll see a 404 status for that file. (200 for all OK, 304 for file not changed)
                        Originally posted by dmjpro
                        And how can i make sure when i make this call then my external file gets loaded completely? ;)
                        a simple test is
                        Code:
                        if (myfunction) alert("myfunction() exists");
                        * Firefox WebDev extension

                        Comment

                        • dmjpro
                          Top Contributor
                          • Jan 2007
                          • 2476

                          #13
                          Originally posted by Dormilich
                          a simple test is
                          Code:
                          if (myfunction) alert("myfunction() exists");
                          * Firefox WebDev extension
                          The code must be like this ..

                          Code:
                          if(typeof myfunction!= 'undefined') //some code
                          Yeah i thought of this..If i need to call that "function" at that moment then ?

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            Originally posted by dmjpro
                            Yeah i thought of this..If i need to call that "function" at that moment then ?
                            throw an Exception* and quit. but usually there's no problem in including an external file**.

                            * seen at MDC
                            Code:
                            throw new TypeError();
                            ** especially if you manage to separate JS code from HTML. some handy functions for this are provided through the event handlers. further you can replace document.write( ) calls by DOM manipulation (createElement( ), appendChild(), …).

                            Comment

                            • Markus
                              Recognized Expert Expert
                              • Jun 2007
                              • 6092

                              #15
                              Wouldn't

                              Code:
                              window.onload = function ()
                              {
                                  myfunction();
                              }
                              overcome that problem too?

                              Or calling it from <body onload="myfunct ion()">?

                              Comment

                              Working...