Mark 123 wrote:
Hi
I am getting an Object Expected error when using the following script. Can
anyone see any syntax error?
<script>
aTemp=location.href.split("?");
if (isnull(aTemp[1])=false)
{
document.forms[0].MyFirstName.value=aTemp[1];
}
if (isnull(aTemp[2])=false)
{
document.forms[0].MyLastName.value=aTemp[2];
}
if (isnull(aTemp[3])=false)
{
document.forms[0].MyEmailAddress.value=aTemp[3];
}
</script>
Two, actually.
1. isnull is undefined. Try using
if( aTemp != null )
2. Cannot assign to function result. Assuming you made a function
called isnull, you would want to use
isnull(aTemp[n])==false
or
!isnull(aTemp[n])
To get the boolean you expect in your if() statements.
I also see what is probably a logical problem:
For the URL:
http://my.example.com/foo.htm?name=value
aTemp will be an array of two values:
aTemp = [
'http://my.example.com/foo.htm',
'name=value'
];
On the other hand, for a (malformed?) URL:
http://my.example.com/foo.htm?avalue...alue?a3rdvalue
then
aTemp = [
'http://my.example.com/foo.htm',
'avalue',
'anothervalue',
'a3rdvalue'
];
Maybe that's what you intended, but isn't it a bit strange?
It would seem better to split location.search on ampersands (into
name/value pairs), then split each result on equal signs (into
individual names and values), then unescape each of those results...
That way you could deal with:
http://my.example.com/foo.htm?MyFirs...stName=Hahn%7e
i.e. this code...
if( aTemp = location.search.split('&')[1] ) {
aTemp = aTemp.split( '&' );
for( c = 0; c < aTemp.length; c++ ) {
aTemp[c] = aTemp[c].split( '=' );
document.forms[0].elements[ unescape( aTemp[c][0] ) ] =
unescape( aTemp[c][1] );
}
}
As a most rough example. It is not the most robust bit of work in the
world (it doesn't handle spaces well), but it can handle very simple
jobs.