Welcome back to this death-defying, heart-pounding race through basic programming for affiliates!
Well, OK, I may be selling it a bit hard there - but seriously, welcome back to Part 2 of the STM tutorial on programming for affiliates who couldn't already program! It's an incredibly useful tool, and one we'll be taking maximum advantage of in this series to make your landing pages sit up and beg.
If you missed Part 1, you can find it here, including details of why you should learn to program as an affiliate, setup for this tutorial, and much more.
Today, we're going to take the basic code we put together in part 1, and extend it to do all sorts of things - including displaying customised images on your lander depending on the characteristics of your incoming visitors, reacting to user input, and more.
But first, we need to learn about one quick but extremely useful feature of Javascript: Variables.
Variables: For When You Absolutely, Positively Don't Want To Type The Same Thing 25 Times
One of the most powerful elements of any programming language is its ability to remember things.
In Part 1 of this tutorial, we used code to pull in text ("Strings", if you remember that) from the URL, and combined that with other text in lines like this:
But what if we want to use that message in a few places (say, in the alert box AND in the text, and maybe again in an exit pop)? Do we have to use that same code each time?
No we don't. We can just take the message, and store it in a variable. Variables are containers for any data we want to use again and again in a program - like a virtual briefcase we can throw information into.
Creating a variable is super-simple. All you do is name it, and put the value you want to store in it after an equals sign. Add this line of code to the end of your lander from last week to store a message in the phonemessage variable:
Once we've done this, we can use that message whenever we want - which also makes our code easier to read. For example, after that line we could then write
and we'd get the same result as in our original code. Note that we don't need to put phonemessage in inverted commas - "" - as it's not a string of text, but something that contains that string.
Try that now with your landing page!
IF Statements - Letting Your Lander Make Choices For You
So, let's get back to our coding. We had successfully persuaded our lander to show the operating system of our phone - but there's a problem.
What if we don't just want to say "IOS" when we see an Apple product, but instead say "Apple iOS"? Or "Apple's Operating System"?
Well, that's where an IF statement comes in. IF statements allow your program to make decisions based on what happens when a visitor arrives - and those decisions can be unique for each visitor. They're an incredibly powerful tool.
And they work just as you'd expect. You define a condition - "the phone's OS is listed as "IOS"", for example. And IF that condition is true, then some code is executed - otherwise, it isn't.
Taking our previous lander (final code here), let's make a couple of modifications.
Firstly, let's split the line which detects the Operating System into two lines, to use Variables (as above). You'll see why in a moment.
Replace
with
phoneos = getParameterByName("operatingsystem");
document.getElementById('operatingsystem').innerHTML = phoneos;
phoneos = getParameterByName("operatingsystem");
if ( phoneos == "IOS" ) { phoneos = "Apple iOS"; }
document.getElementById('operatingsystem').innerHTML = phoneos;

if ( phoneos == "IOS" ) { phoneimage = "apple.jpg" ;}
else if ( phoneos == "Android" ) { phoneimage = "android.jpg"; }
else {phoneimage = "other.jpg"; }
Thanks for the awesome post! I've been trying to learn Javascript for some time, and this seems like an excellent primer!
Thanks Hugh! Your tips are so practical and SO cool - I'm going to implement some of these on my new batch of landers! 
For other coding newbies: I just finished the jQuery tutorial over at Codeacademy - it's short (estimated time = 3 hours if you do it slow) but oh so useful! Not as cool as Caurmen's tutorials but will provide a good basic foundation for anyone new to javascript/jquery. Codeacademy also has tutorials for HTML/CSS, javascript, php, python and ruby if anyone's interested.
http://www.codecademy.com/en/tracks/jquery
After finishing that tutorial @ codecademy.com, the in-depth tutorials @ w3schools would be a great next-step to further your learning:
http://www.w3schools.com/jquery/default.asp
http://www.w3schools.com/jquerymobile/default.asp
And just like codecademy, w3schools has lots of tutorials in other programming languages as well. Have fun!
Amy
@vortex - Yes, I couldn't agree more about CodeAcademy. It works. I used it to brush up my Javascript some time ago, and I've had one of my employees go through their PHP course, after which he was able to go from complete non-programmer to developing useful scripts for me.
If you want to know more after finishing these tutorials, definitely run, do not walk to CodeAcademy.
I'd also strongly recommend their Ruby course - Ruby's the language I use to do most of the scripting I talked about at STM London. http://www.codecademy.com/tracks/ruby
Having just read this, I had this grand idea of having basically only 1 lander. And then passing all these different variables in order to change the look/feel/content of the lander. But then how would you track which combination of variables is performing best? Also is there a way to split test with this method?
For instance say an Android user lands on your page. Can you program it to randomly choose between 2 Android photos? How could you track at that point?
I'd caution against getting too grand with this system - certainly I'd still recommend going for one lander per offer. Data management starts to become a huge overhead, and it's a total pain to change some things (like complete design changes) using Javascript alone. You're better off having a seperate lander there.
Having said that, if you know what lander design sells, and you're only selling one offer, you can get pretty impressively large sets of lander changes going. I know one guy who has over 900 tailored landers for a single offer - each lander's tailored to a particular Adwords keyword.
The best way to split-test variations is to pull ALL variations from the GET data in the URL, and then set up different landers within your tracker for different variations. That might sound a bit complex, so let me take your example and explain how I'd do it.
If I wanted to set up two different images for Android and then switch between them, I'd set up two landers in my tracker for Android, with the following URLs:
androidimage= getParameterByName("androidimage");
phoneos = getParameterByName("operatingsystem");
if ( phoneos == "IOS" ) { phoneimage = "apple.jpg" ;}
else if ( phoneos == "Android" ) { phoneimage = androidimage + ".jpg"; }
else {phoneimage = "other.jpg"; }
That makes perfect sense. And it's a lot simpler than what I was expecting. Going to try implementing something along these lines now. Thanks!
Can I have the final code for this as well? i don't seem to get the image to load
Hey, Thanks for this tutorial!
This sounds to be very effective when building landing pages around targets instead of offers, so that I can change the background-image, <title>, sound or colors to better match the target domain.
Could you please provide a simple example of how to change the background (CSS is inside index.html) and <title> depending on www.domain.com/?target=XXX ??
<title>???</title>
<style>
body{background:url(???)}
</style>
Thanks
Here's Part 3 for anyone trying to follow along.
Is there any reason this code won't execute more than once on the same page in the HTML?
I'm using the span tag to execute
<span id="operatingsystem"></span>
However, when I execute it 2 times, only the first instance on the page will run. If I comment that span out, the second will run
Any hints?

P.s. Im not inserting an image, just using the code to pass the parameter in a few spots on the page..
JS assumes IDs are unique on a page, because HTML specifies they should be. As such, it will only work on the first one.
To make this work, you can do two things:
1) Rename the second instance and run a line of code for each ID.
2) Use getElementsByClassName instead, and then loop through the elements.
The second one is cleaner, the first one is probably simpler unless you plan to have more than two of these elements.
Hope that helps!
Thanks Caurmen - I knew ID's where unique but had no idea that JS would only run once for the first ID,
Classes it is 
Hi Caurmen,
The final code to display image does not work well for me though I copy your code and do nothing else.
Here is how it looks when I open the page in Windows, and IOS also
@deetei - did you add the "?model=iPhone5" parameter to the end of the lander URL, as mentioned in Part 1 of the tutorial? That should make it work.
After fiddling around with the image section of the tutorial, the final code that worked for me was this:
phoneos = getParameterByName("operatingsystem");
if ( phoneos == "MacOS" ) { phoneimage = "apple.jpg" ;}
else if ( phoneos == "Android" ) { phoneimage = "android.jpg"; }
else {phoneimage = "other.jpg"; }
document.getElementById('operatingsystem').innerHT ML = '<img src="' + phoneimage + '">' ;
Hope this helps someone!
(not sure why vBulletin keeps putting a space between HT and ML!)