423,311 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,311 IT Pros & Developers. It's quick & easy.

object expected

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

onClick="location.href='blank.html' +
generateSearchStringFromForm('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.length; 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?



Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Wed, 15 Oct 2003 18:27:15 +0000 (UTC), "Bill" <no@where.com> wrote:
I call a function in my .js file like this:

onClick="location.href='blank.html' +
generateSearchStringFromForm('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.length; 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?


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
Jul 20 '05 #2

P: n/a

"Steve van Dongen" <st*****@hotmail.com> wrote in message
news:00********************************@4ax.com...
On Wed, 15 Oct 2003 18:27:15 +0000 (UTC), "Bill" <no@where.com> wrote:
I call a function in my .js file like this:

onClick="location.href='blank.html' +
generateSearchStringFromForm('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.length; 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 withno 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?


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.


OK, Here is the whole function.

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

for (var i=0; i < document.forms[k].elements.length; 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.indexOf("select") != -1) {
nameValue = escape(element.name) + "=" +
escape(element.options[element.selectedIndex].value);
}

else if (element.type == "checkbox" || element.type == "radio") {
if (element.checked) {
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;
}
Jul 20 '05 #3

P: n/a
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="absmiddle"
onClick="simple('section');
location.href='sectiona.html'">

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


"Steve van Dongen" <st*****@hotmail.com> wrote in message
news:00********************************@4ax.com...
On Wed, 15 Oct 2003 18:27:15 +0000 (UTC), "Bill" <no@where.com> wrote:
I call a function in my .js file like this:

onClick="location.href='blank.html' +
generateSearchStringFromForm('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.length; 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 withno 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?


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

Jul 20 '05 #4

P: n/a
"Bill" <no@where.com> writes:
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="absmiddle"
onClick="simple('section');
location.href='sectiona.html'">

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


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 - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.