"Undefined" error when trying to read array from external JavaScript file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gsuns82
    New Member
    • Mar 2007
    • 58

    "Undefined" error when trying to read array from external JavaScript file

    HI all,

    I tried to read an array from an external javascript file called read.js,The array was declared & as well as defined with some values inside the <script> tag of a html file.

    For an instance:
    test.html:
    --------------
    <html>
    <head>
    <script>
    var a=new Array(1,2,3);
    </script>
    <script type="text/javascript" src="read.js"></script>
    </head>
    </html>

    read.js:
    ----------
    a.sort();
    alert(a[0]);

    test.jsp:
    --------------
    <html>
    <head>
    <script>
    var a=new Array(1,2,3);
    </script>
    <script type="text/javascript" src="read.js"></script>
    </head>
    </html>

    This was successful.
    But i am getting "undefined " as result when i tried test.html as test.jsp.
    I am getting "undefined" message only when i tend to create array
    "var a=new Array(1,2,3)" inside the <script>tag of test.jsp,But i did nt get any "undefined" message if i try the same thing with test.html.

    I am getting problem while running this code at server side.

    can u people guide me in resolving this problem???

    Thanks in advance,
    sundar
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by gsuns82
    HI all,

    I tried to read an array from an external javascript file called read.js,The array was declared & as well as defined with some values inside the <script> tag of a html file.

    For an instance:
    test.html:
    --------------
    <html>
    <head>
    <script>
    var a=new Array(1,2,3);
    </script>
    <script type="text/javascript" src="read.js"></script>
    </head>
    </html>

    read.js:
    ----------
    a.sort();
    alert(a[0]);

    test.jsp:
    --------------
    <html>
    <head>
    <script>
    var a=new Array(1,2,3);
    </script>
    <script type="text/javascript" src="read.js"></script>
    </head>
    </html>

    This was successful.
    But i am getting "undefined " as result when i tried test.html as test.jsp.
    I am getting "undefined" message only when i tend to create array
    "var a=new Array(1,2,3)" inside the <script>tag of test.jsp,But i did nt get any "undefined" message if i try the same thing with test.html.

    I am getting problem while running this code at server side.

    can u people guide me in resolving this problem???

    Thanks in advance,
    sundar
    Java != Javascript

    Moved to Javascript forum

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by gsuns82
      HI all,

      I tried to read an array from an external javascript file called read.js,The array was declared & as well as defined with some values inside the <script> tag of a html file.

      For an instance:
      test.html:
      --------------
      <html>
      <head>
      <script>
      var a=new Array(1,2,3);
      </script>
      <script type="text/javascript" src="read.js"></script>
      </head>
      </html>

      read.js:
      ----------
      a.sort();
      alert(a[0]);

      test.jsp:
      --------------
      <html>
      <head>
      <script>
      var a=new Array(1,2,3);
      </script>
      <script type="text/javascript" src="read.js"></script>
      </head>
      </html>

      This was successful.
      But i am getting "undefined " as result when i tried test.html as test.jsp.
      I am getting "undefined" message only when i tend to create array
      "var a=new Array(1,2,3)" inside the <script>tag of test.jsp,But i did nt get any "undefined" message if i try the same thing with test.html.

      I am getting problem while running this code at server side.

      can u people guide me in resolving this problem???

      Thanks in advance,
      sundar
      hmmm .....
      Code:
      <script type="text/javascript">
      var a=new Array(1,2,3);
      </script>
      And use "<%@page contentType = "text/html;charset=ut f-8"%>" in ".jsp" at the top.

      Debasis Jana

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Changed the thread title to better describe the problem. Please use a good thread title.

        Comment

        Working...