jQuery first steps

August 30th, 2010

Have been intrigued by jquery for a while but have been wary of learning another computer language. Particularly one that looks so different (at least on first inspection) to JavaScript.Decided to take the plunge at the weekend and it turns out it’s not so bad!

The introductory tutorial on the jquery site is actually pretty bad. w3schools is a much better starting point.

It turns out you can combine jquery with existing javascript in a page. Where jquery comes into it’s own is adding graphical effects to a page.

The syntax is along the lines of:

$(page element).event behaviour

I wanted to add a sliding menu to my itunes style navigation menus. Found this tutorial and amended to toggle between expand and contract sub menus

<html>
<head>
<script src='scripts/jquery-1.4.2.min.js'></script>
 <script>
  // Run this code when the page is ready
  $(document).ready(function(){

    // Hide all dd lists apart from the first
    $("dd:not(:first)").hide();

    // Attach this behaviour to the click event of all a elements inside dt tags
    $("dt a").click(function(){

      // show / hide the next element in the page (ie. the list!)
      $(this).parent().next().slideToggle("fast"); // mine
      return false;

     });

   });
 </script>
</head>
<body>
<dl>
	<dt><a href="#">Header 1</a></dt>
	<dd>
	<ul>
		<li><a href="">Item 1</a></li>
		<li><a href="">Item 2</a></li>
       </ul>
	</dd>
        <dt><a href="#">Header 2</a></dt>
	<dd>
	<ul>
		<li><a href="">Item 3</a></li>
		<li><a href="">Item 4</a></li>
       </ul>
	</dd>

</dl>

</body></html>

You can then style the list in any way you like

Single quotes in forms and pdf

August 10th, 2010

Turns out there are encodings for left and right single quotes! I had no idea but someone managed to get some into one of my forms (I suspect copying from word or similar) and caused some problems.

The following will strip left, right and neutral single quotes

oldstring = oldstring.replace(/\u2018/g,"invertedcommasymbol");
oldstring = oldstring.replace(/\u2019/g,"invertedcommasymbol");
oldstring = oldstring.replace(/\u0027/g,"invertedcommasymbol");

UK Pound symbol in fpdf

August 10th, 2010

Was having problems with pound symbols displaying as A£ in pdfs I created with fpdf.

Apparently, fpdf requires text to be in utf8 format.

It’s easily fixed with the following:

$cleantext = utf8_decode($dirtytext);

Sanctuary 17 strategy

August 5th, 2010

OK this isn’t strictly development but it is problem solving!

Sanctuary 17 is an 8-bit style flash game in the style of old ZX Spectrum classics. Part of the retro charm is the breaking of some modern gaming conventions.

  • You don’t have unlimited lives
  • 1 hit will kill you
  • You lose power ups when you die

This can make it very frustrating (at least to start with) but there is a payoff in the form of a good back story and a surprising amount of atmosphere. Another thing which pulls you in is you get better each time you play. I got a bit obsessed by it and eventually got all three endings and thought this might be handy for anyone who’s stuck.

You play in a 6*6 grid and you can see your position by pressing escape. Rooms are randomly allocated except for your starting position which is always 4 from the left and 4 from the top.

There are 2 types of room, ‘node’ rooms – 26 in total each containing a computer node and some bad robots and 10 robot-free ‘equipment’ rooms : The start point, the power station, the library, the water station, the scanner room, the end point and four rooms containing objects (night vision, lightning, big gun and lantern).

There are four types of enemy robots. The best way to kill each is to wait on a straight and shoot them as they reach a corner.

  1. green square ones (can walk behind them and shoot in the back)
  2. blue spider ones (can walk behind them and shoot in the back)
  3. red humanoid ones (turn round and fire when you pass behind them)
  4. green humanoid ones (same as red but two shots to kill)

The purpose of the game is to explore rooms, activate nodes and equipment until you can escape, killing robots on the way as necessary.

Here’s the clincher, (which they don’t tell you!)… As you turn on more nodes or activate equipment, new types of robot appear. So… if you explore all the rooms first without activating anything you get an easy time of it! Even better, you don’t need to explore every room. Just the ones with equipment in them and an additional 9 nodes. The best way to do this is, once you’ve found an equipment room, explore every exit leading from it. If you find another, check all the exits from that and so on. It’s sometimes possible to find a path leading directly from the start point to the exit, passing all the equipment rooms without a single robot room!.

Once you’ve found all the equipment rooms, activate a node. This saves your position. If you die now, don’t carry on. Refresh the page and click continue. This is a bit of a cheat and not entirely necessary but if you want a failsafe this is the way to go.

Once you’ve activated 9 nodes in total the exit will open. This is where the three endings come in.

  1. If you go through the door now, only you escape
  2. If you go back to the start point, you and your band of followers escape
  3. If you activate all the equipment (in order water, power, scanner) and go back to the start point, all the survivors will follow you to freedom!

Fruitful way to spend time? Possibly not. Hugely satisfying to beat? Definitely.

Preventing javascript injection

August 4th, 2010

Need to prevent users deciphering function calls and executing functions from the address bar.

Not a major issue at the moment as no pages are open to the public but need to sort out before bigger roll outs.

First thought… add an owner variable to all tables. Only allow changes by owner in the php updater file.

Also need to add session validation to updater file.