Can't Return any values from Functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Zhichun Pu

    Can't Return any values from Functions

    Hi guys,

    I can't seem to get anything returned from functions.

    If I use the <script src = ... > command, nothing works. But if I
    define the functions locally within the file, then it works.

    Something simple:

    function abc() { return 99; }

    If I tried to access this function by placing it inside a .js file,
    and then including it, it returns "undefined" . Everything else works
    except for returned values...

    If I define this function locally, it returns correctly the value of
    99.

    What is going on?? This is so weird.

    Zhichun
  • Grant Wagner

    #2
    Re: Can't Return any values from Functions

    Zhichun Pu wrote:
    [color=blue]
    > Hi guys,
    >
    > I can't seem to get anything returned from functions.
    >
    > If I use the <script src = ... > command, nothing works. But if I
    > define the functions locally within the file, then it works.
    >
    > Something simple:
    >
    > function abc() { return 99; }
    >
    > If I tried to access this function by placing it inside a .js file,
    > and then including it, it returns "undefined" . Everything else works
    > except for returned values...
    >
    > If I define this function locally, it returns correctly the value of
    > 99.
    >
    > What is going on?? This is so weird.
    >
    > Zhichun[/color]

    Without more information it's hard to know what the problem is. However,
    one mistake some people make is to include <script></script> tags in
    their external .js file. The .js file should only contain JavaScript, it
    should contain no HTML tags or comments.

    So if your inline code is:

    <script type="text/javascript">
    <!--
    function abc() { return 99; }
    //-->
    </script>

    Then the external .js file should contain only:

    function abc() { return 99; }

    --
    | Grant Wagner <gwagner@agrico reunited.com>

    * Client-side Javascript and Netscape 4 DOM Reference available at:
    *


    * Internet Explorer DOM Reference available at:
    *
    Gain technical skills through documentation and training, earn certifications and connect with the community


    * Netscape 6/7 DOM Reference available at:
    * http://www.mozilla.org/docs/dom/domref/
    * Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
    * http://www.mozilla.org/docs/web-deve...upgrade_2.html


    Comment

    • Zhichun Pu

      #3
      Re: Can't Return any values from Functions

      I tried writing a few more test scripts and they all worked.... maybe
      it was the script I was using that didn't work.

      function alertSize() {
      var myWidth = 0, myHeight = 0;
      if( typeof( window.innerWid th ) == 'number' ) {
      //Non-IE
      myWidth = window.innerWid th;
      myHeight = window.innerHei ght;
      } else {
      if( document.docume ntElement &&
      ( document.docume ntElement.clien tWidth ||
      document.docume ntElement.clien tHeight ) ) {
      //IE 6+ in 'standards compliant mode'
      myWidth = document.docume ntElement.clien tWidth;
      myHeight = document.docume ntElement.clien tHeight;
      } else {
      if( document.body && ( document.body.c lientWidth ||
      document.body.c lientHeight ) ) {
      //IE 4 compatible
      myWidth = document.body.c lientWidth;
      myHeight = document.body.c lientHeight;
      }
      }
      }
      window.alert( 'Width = ' + myWidth );
      window.alert( 'Height = ' + myHeight );
      }

      I pulled it off a website. It's supposed to get the width and height
      of a browser's window. I just did "return [myWidth, myHeight];" and it
      didn't work. I tried to return a single value and that didn't work
      either.

      Maybe it also had to do with the DynAPI package I was using?
      Javascript has changed so much since I last programmed in it....

      Any comments + ideas would be greatly appreciated. Thanks to all those
      who tried to help =)

      Zhichun



      schavager@hotma il.com (Zhichun Pu) wrote in message news:<e8fb36f7. 0307152041.bc7c 397@posting.goo gle.com>...[color=blue]
      > Hi guys,
      >
      > I can't seem to get anything returned from functions.
      >
      > If I use the <script src = ... > command, nothing works. But if I
      > define the functions locally within the file, then it works.
      >
      > Something simple:
      >
      > function abc() { return 99; }
      >
      > If I tried to access this function by placing it inside a .js file,
      > and then including it, it returns "undefined" . Everything else works
      > except for returned values...
      >
      > If I define this function locally, it returns correctly the value of
      > 99.
      >
      > What is going on?? This is so weird.
      >
      > Zhichun[/color]

      Comment

      Working...