Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,641 Times in 4,603 Posts
    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.

  • Users who have thanked Old Pedant for this post:

    WhatABoringName (11-14-2012)

  • #3
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    That's what I needed, makes for a cleaner list to manage. Much appreciated!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •