Get FirstName, LastName and EmailAddress from URL

M

Mark 123

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>
 
R

RobG

Mark said:
Hi

I am getting an Object Expected error when using the following script. Can
anyone see any syntax error?



<script>

The type attribute is required:

aTemp=location.href.split("?");

if (isnull(aTemp[1])=false)
--------^-------------^

I think you're after Douglas Crockfords 'isNull()'.

<URL:http://www.crockford.com/javascript/remedial.html>

isNull() (if you implement it) returns 'true' if the expression is
null, so you have the 'sense' of the comparison wrong.

But ... you have an assignment instead of a comparison - gosh, all that
in just one line! :)

If you want the code to execute if aTemp[1] *is not* null, then:

if ( aTemp[1] ) {
// do stuff
}

will do the trick.
{
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];
}

I would nest the ifs so that if one fails, subsequent ifs are skipped
(and keep a reference to the from for brevity):

if ( aTemp[1] ) {
var f = document.forms[0];
f.MyFirstName.value = aTemp[1];

if ( aTemp[2] ) {
f.MyLastName.value = aTemp[2];

if ( aTemp[3] ) {
f.MyEmailAddress.value = aTemp[3];
}
}
}

But even then you are making assumptions that the values of aTemp are
suitable - hopefully you have don some testing or validation of the
values before using them.
 
E

Evertjan.

commercial wrote on 04 jul 2005 in comp.lang.javascript:
var aMayBe, bSplit, iUseFul

aMayBe = location.href.split("?");

bSplit = iUseFul = aMayBe.length

if(bSplit && iUseFul > 1){

aMayBe.shift()

for(var i = 0 ; i < aMayBe.length ; i++)
document.forms[0].elements.value=aMayBe;

}

You need more than 1 array values (those from the right site of the
split). So you get rid of the first.
Now let it iterate over you form fields by index.
Until you drop, or your PC.

Of course this can be shortened.


[not sending HTML on usenet would be best shortening ;-)]

aMayBe = location.href.split("?");
for (var i = 1 ; i < aMayBe.length ; i++)
document.forms[0].elements.value = aMayBe;
 
E

Evertjan.

Evertjan. wrote on 04 jul 2005 in comp.lang.javascript:
aMayBe = location.href.split("?");
for (var i = 1 ; i < aMayBe.length ; i++)
document.forms[0].elements.value = aMayBe;


However, I think this is ment:

mySplit = location.href.split('?');
if (mySplit.length>1){
aMayBe = mySplit.split('&')
for (var i = 0 ; i < aMayBe.length ; i++)
document.forms[0].elements.value = aMayBe;
}
 
C

Christopher J. Hahn

Mark said:
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?anothervalue?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?MyFirstName=~Chris&MyLastName=Hahn~

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.
 
C

Christopher J. Hahn

Christopher J. Hahn wrote:
[snip]
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?MyFirstName=~Chris&MyLastName=Hahn~

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.


This line
document.forms[0].elements[ unescape( aTemp[c][0] ) ] =
Should read
document.forms[0].elements[ unescape( aTemp[c][0] ) ].value =
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top