homepage Welcome to WebmasterWorld Guest from 23.20.63.27
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
If else Javascript
Rhys




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

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




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

<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




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

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




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

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




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

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




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

Thanks too Fotiman

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved