Randomize Web Images with JavaScript

July 22nd, 2010  / Author: cwl

This is documented in many other places on the Internet, but it can’t hurt to have too many tutorials.

You can jazz up your web site with a bit of JavaScript to randomly insert images on your page. I’m doing this on my masthead, as one example.

The first requirement is to build a list of images:

myPix = new Array("images/bridge.jpg", "images/butterfly1.jpg", "images/cats.jpg", "images/ducks.jpg", "images/field1.jpg", "images/field2.jpg", "images/flowers2.jpg", "images/flowers3.jpg", "images/flowers4.jpg"
                  );

Then write the code to select one at random:

imgCt = myPix.length;
function choosePic() {
  if(document.images) {
    randomNum = Math.floor((Math.random() * imgCt));
    document.myPic.src = myPix[randomNum];
  }
}

Finally, you need to set up the HTML to hold the image:

<img id='frontphoto' name='myPic' src='images/bg.gif' />

The name=’myPic’ relates to the JS “document.myPic.src” declaration. The id=’frontphoto’ definition in the <img> tag is the CSS magic that sizes the image. And the ‘images/bg.gif’ is a placeholder image that gets overwritten by the selected image.

That’s it – you can poke around the web site and watch it work.

De-clutter Your Rails Views, Use Partials For JavaScript SEO

July 9th, 2010  / Author: cwl

If your Marketing or SEO guy is out of control (and when are they not?), you may be finding yourself stuffing insane amounts of JavaScript into your Rails views in order to track… Continue reading

IE 8 Compatibility View

May 14th, 2010  / Author: cwl

As a UNIX / Linux bigot, I don’t use Internet Explorer much. As a web developer, I need to test as many browsers as possible. The matrix for all the possible browser / version combinations… Continue reading

Rails Time Extension Edge Case

January 31st, 2010  / Author: cwl

I love the extensions that Rails has for the Ruby Time object (see ActiveSupport::CoreExtensions::Integer::Time) – you can do calculations like Time.now + 1.month, (4.months + 5.years).from_now, and other cool things.

In my last post… Continue reading

Ouch. Tricky Rails Multiparameter Bug In Attribute Writers

January 28th, 2010  / Author: cwl

I’m building a web site with a subscription-based payment system using ActiveMerchant. The first rule is, don’t put any user financial information into your local database, just pass the user input to your payment system… Continue reading

Multiple Rails Apps Under Passenger

December 30th, 2009  / Author: cwl

My development server is on a home LAN behind a firewall, and I don’t want to make it a DNS sub-domain. I have directed my router to issue fixed IP addresses through DHCP, so I… Continue reading

OpenSolaris Under VMware

December 17th, 2009  / Author: admin

OpenSolaris looks pretty cool, so I’m giving it a spin as a VMware Virtual Machine. The trick is to set up the new VM as a Solaris 10 system – that wasn’t obvious to me… Continue reading

Rails and PHP under Ubuntu Apache

December 15th, 2009  / Author: admin

I have Ubuntu 9.10 (aka Karmic Koala) running on my laptop. It’s rapidly becoming my portable development machine, so I set up the Rails environment right away. I have decided to add PHP to the… Continue reading | 2 Comments

Close Enough for Now

November 24th, 2009  / Author: admin

I think the format is a bit better. On to other things…

- Changed the image to something I own

- font-family: Verdana,Tahoma,Arial,sans-serif;

- Changed text color to black

- Changed… Continue reading

Theme Thrashing

November 19th, 2009  / Author: admin

I’ve never been entirely satisfied with the way my blog looks – it still feels “hard to read”, and I’m not sure how to fix that, except to experiment with some other two-column, fixed-width WP… Continue reading | 1 Comment