Skip to content

Instantly share code, notes, and snippets.

@derpyherp
Created January 8, 2022 11:51
Show Gist options
  • Save derpyherp/536160a3510df389dfa4b31af6032b3c to your computer and use it in GitHub Desktop.
Save derpyherp/536160a3510df389dfa4b31af6032b3c to your computer and use it in GitHub Desktop.
Very basic JavaScript banner rotator.
//probably want a window width of at least 468px to display a 468x60 banner
if (window.matchMedia("(min-width: 468px)").matches) {
var banner;
//the first number should equal the number of banners
switch(Math.floor((Math.random() * 3) + 1)) {
case 1:
banner = ["https://destination1.com", "https://destination1.com/banner.gif"];
break;
case 2:
banner = ["https://destination2.com", "https://destination2.com/banner.png"];
break;
default:
banner = ["https://destination3.com", "https://destination3.com/banner.jpg"];
}
if (banner) {
document.getElementById('banner-container').innerHTML = '<a href="'+banner[0]+'" target="_blank" rel="nofollow noopener noreferrer"><img src="'+banner[1]+'" height="468" width="60" alt="Advertisement" /></a>';
}
}
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<div id="banner-container" style="width:468px;height:60px;></div>
<script src="javascript-banner-rotator.js" async></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment