object expected

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

    object expected

    I call a function in my .js file like this:

    onClick="locati on.href='blank. html' +
    generateSearchS tringFromForm(' section')"

    where section is the name of my form.

    The function is defined as follows:

    myFunction(k) {
    for (var i=0; i < document.forms[k].elements.lengt h; i++) {
    element = document.forms[k].elements[i];
    //do stuff with each element
    }
    }

    The problem is I'm getting the following error -

    Error: Object expected
    Code: 0

    Surely the above code is OK?

    I seem to get this error alot, even though it appears to work sometimes with
    no complaint.

    If there is an error in the code of another function in the same .js page
    would this cause the call to this particular function to return such an
    error?







  • Steve van Dongen

    #2
    Re: object expected

    On Wed, 15 Oct 2003 18:27:15 +0000 (UTC), "Bill" <no@where.com > wrote:
    [color=blue]
    >I call a function in my .js file like this:
    >
    >onClick="locat ion.href='blank .html' +
    > generateSearchS tringFromForm(' section')"
    >
    >where section is the name of my form.
    >
    >The function is defined as follows:
    >
    >myFunction(k ) {
    > for (var i=0; i < document.forms[k].elements.lengt h; i++) {
    > element = document.forms[k].elements[i];
    > //do stuff with each element
    > }
    >}
    >
    >The problem is I'm getting the following error -
    >
    >Error: Object expected
    >Code: 0
    >
    >Surely the above code is OK?
    >
    >I seem to get this error alot, even though it appears to work sometimes with
    >no complaint.
    >
    >If there is an error in the code of another function in the same .js page
    >would this cause the call to this particular function to return such an
    >error?[/color]

    Usually when you get Object Expected on line 0 it means you are
    calling a function in the event handler but your script cannot be
    parsed because of a syntax error.

    It's unlikely you've posted enough code for anyone to help you, but
    what you have posted has one obvious syntax error -- function
    definitions start with the "function" keyword.

    Regards,
    Steve

    Comment

    • Bill

      #3
      Re: object expected


      "Steve van Dongen" <stevevd@hotmai l.com> wrote in message
      news:005rovcrhs rm2n17ge46jitjb a5ir7nicn@4ax.c om...[color=blue]
      > On Wed, 15 Oct 2003 18:27:15 +0000 (UTC), "Bill" <no@where.com > wrote:
      >[color=green]
      > >I call a function in my .js file like this:
      > >
      > >onClick="locat ion.href='blank .html' +
      > > generateSearchS tringFromForm(' section')"
      > >
      > >where section is the name of my form.
      > >
      > >The function is defined as follows:
      > >
      > >myFunction(k ) {
      > > for (var i=0; i < document.forms[k].elements.lengt h; i++) {
      > > element = document.forms[k].elements[i];
      > > //do stuff with each element
      > > }
      > >}
      > >
      > >The problem is I'm getting the following error -
      > >
      > >Error: Object expected
      > >Code: 0
      > >
      > >Surely the above code is OK?
      > >
      > >I seem to get this error alot, even though it appears to work sometimes[/color][/color]
      with[color=blue][color=green]
      > >no complaint.
      > >
      > >If there is an error in the code of another function in the same .js page
      > >would this cause the call to this particular function to return such an
      > >error?[/color]
      >
      > Usually when you get Object Expected on line 0 it means you are
      > calling a function in the event handler but your script cannot be
      > parsed because of a syntax error.
      >
      > It's unlikely you've posted enough code for anyone to help you, but
      > what you have posted has one obvious syntax error -- function
      > definitions start with the "function" keyword.[/color]

      OK, Here is the whole function.

      //set k to FORM NAME or INDEX to extract its field vlaues
      function generateSearchS tringFromForm(k ) {
      var element;
      var srchStr = "?";
      var last = document.forms[k].elements.lengt h - 1;

      for (var i=0; i < document.forms[k].elements.lengt h; i++) {

      element = document.forms[k].elements[i];

      if (element.type == "text" || element.type == "textarea" ||
      element.type == "hidden") {
      nameValue = escape(element. name) + "=" + escape(element. value);
      }

      else if (element.type.i ndexOf("select" ) != -1) {
      nameValue = escape(element. name) + "=" +
      escape(element. options[element.selecte dIndex].value);
      }

      else if (element.type == "checkbox" || element.type == "radio") {
      if (element.checke d) {
      nameValue = escape(element. name) + "=" +
      escape(element. value);
      } else continue
      } else continue

      srchStr += nameValue;

      //only add a '&' if there are more parameters
      if (i < last)
      srchStr += "&";
      }

      return srchStr;
      }


      Comment

      • Bill

        #4
        Re: object expected

        Actually, I replaced the call to this simple function:

        function simple(k) {
        alert("simple says: " + k);
        }

        called like this

        <img src="images/Next-button.gif" width="90" height="32" border="0"
        align="absmiddl e"
        onClick="simple ('section');
        location.href=' sectiona.html'" >

        and I get the same Object expected error for that line.








        "Steve van Dongen" <stevevd@hotmai l.com> wrote in message
        news:005rovcrhs rm2n17ge46jitjb a5ir7nicn@4ax.c om...[color=blue]
        > On Wed, 15 Oct 2003 18:27:15 +0000 (UTC), "Bill" <no@where.com > wrote:
        >[color=green]
        > >I call a function in my .js file like this:
        > >
        > >onClick="locat ion.href='blank .html' +
        > > generateSearchS tringFromForm(' section')"
        > >
        > >where section is the name of my form.
        > >
        > >The function is defined as follows:
        > >
        > >myFunction(k ) {
        > > for (var i=0; i < document.forms[k].elements.lengt h; i++) {
        > > element = document.forms[k].elements[i];
        > > //do stuff with each element
        > > }
        > >}
        > >
        > >The problem is I'm getting the following error -
        > >
        > >Error: Object expected
        > >Code: 0
        > >
        > >Surely the above code is OK?
        > >
        > >I seem to get this error alot, even though it appears to work sometimes[/color][/color]
        with[color=blue][color=green]
        > >no complaint.
        > >
        > >If there is an error in the code of another function in the same .js page
        > >would this cause the call to this particular function to return such an
        > >error?[/color]
        >
        > Usually when you get Object Expected on line 0 it means you are
        > calling a function in the event handler but your script cannot be
        > parsed because of a syntax error.
        >
        > It's unlikely you've posted enough code for anyone to help you, but
        > what you have posted has one obvious syntax error -- function
        > definitions start with the "function" keyword.
        >
        > Regards,
        > Steve[/color]


        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: object expected

          "Bill" <no@where.com > writes:
          [color=blue]
          > Actually, I replaced the call to this simple function:
          >
          > function simple(k) {
          > alert("simple says: " + k);
          > }
          >
          > called like this
          >
          > <img src="images/Next-button.gif" width="90" height="32" border="0"
          > align="absmiddl e"
          > onClick="simple ('section');
          > location.href=' sectiona.html'" >
          >
          > and I get the same Object expected error for that line.[/color]

          Then the function is probably not defined. How is the code included?
          In a script tag on the page or externally? Are you sure you have
          spelled type="text/javascript" correctly? (I always miss an "a").

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          Working...