Welcome to WebmasterWorld Guest from 54.226.246.160

Forum Moderators: open

Message Too Old, No Replies

If else Javascript

     

Rhys

9:28 am on Feb 13, 2009 (gmt 0)

10+ Year Member



I have this code which checks to see if a given term exists in the referrer name; and I now need to modify the script to do an action if the term is NOT there

Existing Script:

<script language="JavaScript">
<!--
var myArray = [];
myArray[0]= 'domain1';
myArray[1]= 'domain2';
myArray[2]= 'domain3';
myArray[3]= 'domain4';
myArray[4]= 'domain5';
for(var i=0;
i<5;
i++) {
if (document.referrer.indexOf(myArray[i]) > -1)
{
document.write('Some Text inserted here');
}
// insert else here?
}
//-->
</script>

Something like:
else document.write('Some other Text to insert here');

My problem is that when I put in the else clause, it inserts the alternate text 5 times

[edited by: eelixduppy at 5:35 pm (utc) on Feb. 14, 2009]
[edit reason] formatting [/edit]

daveVk

10:54 am on Feb 13, 2009 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



<script language="JavaScript">
<!--
var myArray = [];
myArray[0]= 'domain1';
myArray[1]= 'domain2';
myArray[2]= 'domain3';
myArray[3]= 'domain4';
myArray[4]= 'domain5';
// or instead of above, var myArray = [ 'domain1', 'domain2' ... ];
var found = false;
for(var i=0;
i<5;
i++) {
if (document.referrer.indexOf(myArray[i]) > -1)
{
document.write('Some Text inserted here');
found = true;
}
// insert else here?
}
if ( found === false ) { document.write('Some Other Text inserted here'); }
}

//-->
</script>

Rhys

11:20 am on Feb 13, 2009 (gmt 0)

10+ Year Member



Thanks very much DaveVK

It works perfectly - You are the MAN.

Here is the entire code for the benefit of future readers.


<script language="JavaScript">
<!--
var myArray = [];
myArray[0]= 'domain1';
myArray[1]= 'domain2';
myArray[2]= 'domain3';
myArray[3]= 'domain4';
myArray[4]= 'domain5';
var found = false;
for(var i=0;
i<5;
i++) {
if (document.referrer.indexOf(myArray[i]) > -1)
{
document.write('Some Text inserted here');
found = true;
}
if ( found === false ) { document.write('Some Other Text inserted here'); }
}
//-->
</script>

:)
P.S. The WW program keeps editing the second line of the array from: myArray[1]= 'domain2';
to: myArray= 'domain2';
- very weird!

[edited by: eelixduppy at 5:36 pm (utc) on Feb. 14, 2009]
[edit reason] formatting [/edit]

Fotiman

3:17 pm on Feb 13, 2009 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Since you appear to be initializing the array immediately anyway, you could rewrite it like this:


<script type="text/javascript">
var myArray = [
'domain1',
'domain2',
'domain3',
'domain4',
'domain5'
];
var i, numDomains = myArray.length, found = false;
for (i = 0; i < numDomains; i++) {
if (document.referrer.indexOf(myArray[i]) > -1) {
document.write('Some Text inserted here');
found = true;
}
}
if (!found) {
document.write('Some Other Text inserted here');
}
</script>

Note also that the "language" attribute is invalid. You should be using the type attribute instead. Also, don't put HTML comments inside the script tag. This is left over Netscape 1 which is long dead.
Lastly, this bit of code will need to be placed in your HTML page because of the document.write call. A better alternative would be to keep this totally separated from your markup and then use DOM methods to insert the text, but for now this should work for you.

[edited by: Fotiman at 3:22 pm (utc) on Feb. 13, 2009]

Fotiman

3:21 pm on Feb 13, 2009 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Here's another way you could write it:


<script type="text/javascript">
var myArray = [
'domain1',
'domain2',
'domain3',
'domain4',
'domain5'
];
var i, numDomains = myArray.length, found = false;
for (i = 0; i < numDomains; i++) {
if (document.referrer.indexOf(myArray[i]) > -1) {
found = true;
}
}
document.write((found ? 'Some Text inserted here' : 'Some Other Text inserted here'));
</script>

Rhys

4:19 am on Feb 14, 2009 (gmt 0)

10+ Year Member



Thanks too Fotiman

Your script is a bit simpler than the one above. I have installed it and it runs perfectly, thank You

 

Featured Threads

Hot Threads This Week

Hot Threads This Month