Coding Compendium
A free 100-page ebook collecting my projects and tutorials for Raspberry Pi, micro:bit, Scratch and Python. Simply join my newsletter to download it.
This is part of the Javascript Smart Marketing Toolkit.
Using the toolkit, it is possible for you to manage your banner adverts and local storage (roughly speaking, equivalent to cookies) without having to do any detailed programming yourself. Once you've edited your banner details into the toolkit (see the remarks in the toolkit code for where to do this), you can use these lines of code to have your adverts automatically cycled:
<div id="banner"></div>
<script>document.querySelector("#banner").innerHTML = output_banner(least_views())</script>
Although this code was originally developed when banner adverts were all the rage (!), it can also be used for cycling other promotional content within your website.
Here's an example of this code in action. When you revisit this page, you'll see the next ad in this slot. I've also added the banner code at the foot of the other demonstration pages. Whatever order you visit the demonstration pages in, the adverts will cycle in the right order.
Function | What it does |
---|---|
set_up() | The set_up() routine must be run when the page loads. |
output_banner(number) | After the hard work of editing the set_up() routine, all you need to do to display a banner advert (and automatically keep track of how many times it's been displayed) is to call this routine with the appropriate banner number. You'll need to wrap this is a document.write() or use the more elegant example code above. |
least_views() | This will tell you the number of the banner advert that has been shown the fewest times. |
© Sean McManus. All rights reserved.
Visit www.sean.co.uk for free chapters from Sean's coding books (including Mission Python, Scratch Programming in Easy Steps and Coder Academy) and more!
A free 100-page ebook collecting my projects and tutorials for Raspberry Pi, micro:bit, Scratch and Python. Simply join my newsletter to download it.
Web Design in Easy Steps, now in its 7th Edition, shows you how to make effective websites that work on any device.
Power up your Microsoft Excel skills with this powerful pocket-sized book of tips that will save you time and help you learn more from your spreadsheets.
This book, now fully updated for Scratch 3, will take you from the basics of the Scratch language into the depths of its more advanced features. A great way to start programming.
Code a space adventure game in this Python programming book published by No Starch Press.
Discover how to make 3D games, create mazes, build a drum machine, make a game with cartoon animals and more!