Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-14-2012, 08:28 PM   PM User | #1
WhatABoringName
New to the CF scene

 
Join Date: Nov 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
WhatABoringName is an unknown quantity at this point
indexOf() function with a variable string?

Greetings,

for the following webpage I created a piece of Javascript to fill in the missing images: Link

Code:
var imageGet = document.getElementsByClassName('art');
var i = imageGet.length;
var a;

while (--i>=0)
	{

	a = imageGet[i].parentNode.parentNode.getAttribute('href');


		if (a.indexOf('/Larks%27+Tongues+in+Aspic') > -1)
		{
		imageGet[i].src = 'http://i1325.photobucket.com/albums/u622/last_fm_projeKct/Last%20FM%20covers/64%20x%2064/ltia.jpg'
		};


		if (a.indexOf('/Three+of+a+Perfect+Pair') > -1)
		{
		imageGet[i].src = 'http://i1325.photobucket.com/albums/u622/last_fm_projeKct/Last%20FM%20covers/64%20x%2064/3oapp.jpg'
		};

	};
While this actually does what is supposed to happen, I was wondering if there's a neater way of including more possible strings for the indexOf()-function, without making a new 'if'-statement for every piece of string I'd want to include eventually.

Thanks for taking the time to read this.

Cheers!
WhatABoringName is offline   Reply With Quote
Old 11-14-2012, 09:27 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Since it seems that each "if" test has to then produce a different URL for the .src property, I don't see how you can avoid doing one "if" per string with one URL per "if".

But at least you could do it using an array:
Code:
    var addPics = [
        [ "/Larks%27+Tongues+in+Aspic", "ltia.jpg" ],
        [ "/Three+of+a+Perfect+Pair", "3oapp.jpg" ],
        ... add as many more as needed ...
    ]
    var baseURL = "http://i1325.photobucket.com/albums/u622/last_fm_projeKct/Last%20FM%20covers/";

    while (--i>=0)
    {
        var image = imageGet[i];
        var a = image.parentNode.parentNode.getAttribute('href');
        for ( var p = 0; p < addPics.length; ++p )
        {
            if (a.indexOf( addPics[p][0] ) >= 0 ) 
            { 
                image.src = baseURL + addPics[p][1]; 
                break; // out of for loop...presumably you will only change image.src once
            }
        }
    }
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
WhatABoringName (11-14-2012)
Old 11-14-2012, 09:47 PM   PM User | #3
WhatABoringName
New to the CF scene

 
Join Date: Nov 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
WhatABoringName is an unknown quantity at this point
That's what I needed, makes for a cleaner list to manage. Much appreciated!
WhatABoringName is offline   Reply With Quote
Reply

Bookmarks

Tags
array, indexof

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:51 AM.


Advertisement
Log in to turn off these ads.