Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How to create landing pages for A/B test with JavaScript?

Hi,

I am creating a website and want to have couple of landing pages before deciding the permanent one. It's a pretty simple website just with HTML&CSS. So, the questions is:

How can I link and redirect visitors to randomised landing pages by using JavaScript, when they enter to the site's home page?

1 Answer

The following code should do it:

var redirect, pageOptions = ['a.html', 'b.html'];
if (Math.random() < .5){
    redirect = pageOptions[0];
} else {
    redirect = pageOptions[1];
}
window.location.assign(redirect);

Users will be able to see the different URL in their browser. If you don't want that to happen, you'll need to serve different content via server code.