Gareth Heyes |
21 July 2023 at 14:00 UTC
I love it when Chrome releases a new feature, I especially like it when it is experimental. In this post I’m going to show you how I created Tic Tac Toe (Noughts and crosses) with HTML, using one of those experimental features. Creating the game in CSS is trivial – as I’ve proven when I created a
3D first person shooter
– so I wanted to challenge myself to create a game with just HTML.
How it started
It all started when Chrome implemented popovers in HTML
. My first action was to test it for XSS vectors and, with the help of the XSS community, we found some pretty cool vectors that allow you to
exploit hidden inputs and meta tags
.
You would think this is where the story ends, but that’s not the case. An idea popped into my head and once it’s there, my brain goes into overdrive and I’m compelled to do it. The idea went like this: if you could create a popup using pure HTML, then you have some form of state e.g. the popup is showing or not. I thought you could then use this state information to create a pure HTML game and for whatever reason, Tic Tac Toe stuck in my head. Idea now fully committed, I spent my lunch hours creating the game.
Popovers are quite simple; you use a popover attribute to define the element you want to use as a popover element, which is then hidden. Then you use a popovertarget attribute in another element, usually either an input or a button, to link to the element to show it when you click the button:
The basic idea
Can you see where this is going? You could use the popups to show a Tic Tac Toe board, and then use the ids for each move. Putting this theory to the test, I did a little experiment to see if you could use multiple popups one after another and it worked! The basic idea now confirmed, I then started to write some code that would generate the permutations. At first I used tables to construct the board, and buttons for the choices:
for(i=1;i<10;i++) {
html +=`
${i==1?'':''} | ${i==2?'':''} | ${i==3?'':''} |