Oh, as an Aside: Short Ajax Script (From the 13th of May)

I wanted to see how small you could make a fully functional AJAX script that worked cross-browser and degraded gracefully, so I went through an old custom AJAX script and made it as small as I possibly could. In the resulting AJAX scripts, the post version is 410 characters and the GET version is only 359 characters long. The scripts are fully functional and accept the following parameters: URL, DATA (in string form), and ELEMENT (to update).

The scripts could be a little smaller, but it would really kill readability.

“Get” AJAX Script

  1. function a(l,d,u){
  2. try{r = new XMLHttpRequest();}catch(e){try {r = new ActiveXObject('Msxml2.XMLHTTP');}catch(e){r = new ActiveXObject('Microsoft.XMLHTTP');}}
  3. if(r){
  4. r.onreadystatechange = function() {if (r.readyState == 4 && r.status == 200){document.getElementById(u).innerHTML = r.responseText;}}
  5. r.open('GET', l+'?'+d, true);r.send(d);
  6. }
  7. }

“Post” AJAX Script

  1. function b(l,d,u){
  2. try{r = new XMLHttpRequest();} catch(e){try {r = new ActiveXObject('Msxml2.XMLHTTP');} catch(e){r = new ActiveXObject('Microsoft.XMLHTTP');}}
  3. if(r){
  4. r.onreadystatechange = function() {if (r.readyState == 4 && r.status == 200){document.getElementById(u).innerHTML=r.responseText;}}
  5. r.open('POST', l, true);r.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');r.send(d);
  6. }
  7. }

Combined AJAX Script

This combined script also accepts a fourth parameter ‘p’ that should evaluate true if the data is to be sent by post.

  1. function a(l,d,u,p){
  2. try{r = new XMLHttpRequest();}catch(e){try {r = new ActiveXObject('Msxml2.XMLHTTP');}catch(e){r = new ActiveXObject('Microsoft.XMLHTTP');}}
  3. if(r){
  4. r.onreadystatechange = function() {if (r.readyState == 4 && r.status == 200){document.getElementById(u).innerHTML = r.responseText;}}
  5. if(p){r.open('POST', l, true);r.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');}else{r.open('GET', l+'?'+d, true);}
  6. }
  7. }

Demo: (Sorry but you will have to go to the full page so the JavaScript is loaded.)

Continue reading. »

Oh, as an Aside: Displaying WordPress categories in a horizontal dropdown menu. (From the 16th of January)

One of my readers recently asked how I created my horizontal menu bar: the short answer is by mixing CSS and Javascript.

The first step is to get WordPress to display the menu as a hierarchical list without a title. <?php wp_list_categories('sort_column=name&sort_order=asc&style=list&children=true&hierarchical=true&title_li=0'); ?>

We then wrap this WordPress code in the following so we can style it.

  1. <div style="text-align:center;">
  2. <ul id="menu" style="padding:0; margin:0;">
  3. <?php wp_list_categories('sort_column=name&sort_order=asc&style=list&children=true&hierarchical=true&title_li=0'); ?>
  4. </ul>
  5. </div>

I added this to my header.php, but you can add it anywhere you want it to appear.

The CSS is fairly simple and you just need to add it to your theme’s style.css file.

  1. ul#menu {
  2. margin: 0;
  3. padding: 0;
  4. list-style: none;
  5. width: 100%;
  6. font-size:1.2em;
  7. }
  8. ul#menu li {
  9. float: left;
  10. padding: 0;
  11. margin: 0;
  12. border-right:solid 1px #fff;
  13. }
  14. ul#menu ul li {
  15. float: none;
  16. position: relative;
  17. border-bottom: 1px solid #7EAED7; /* fixes gap problem in IE */
  18. border-left: 1px solid #FFF;
  19. z-index:1000;
  20. }
  21. ul#menu li ul {
  22. margin: 0;
  23. padding: 0;
  24. display:none;
  25. list-style: none;
  26. position: absolute;
  27. background: #9CC;
  28. }
  29. ul#menu ul ul{
  30. margin-left: .2em;
  31. position: absolute;
  32. top: 0; /* if using borders, -1px to align top borders */
  33. left: 100%;
  34. }
  35. ul#menu * a:hover, ul#menu li a:active{
  36. background:#7EAED7 !important;
  37. color: #FFFFFF;
  38. }
  39. ul#menu li a:link,
  40. ul#menu li a:visited,
  41. ul#menu li a:hover,
  42. ul#menu li a:active{
  43. display: block;
  44. padding: .2em .3em;
  45. text-decoration: none;
  46. background: #5587B3;
  47. color: #FFFFFF;
  48. }
  49. ul#menu ul li a:link,
  50. ul#menu ul li a:visited,
  51. ul#menu ul li a:hover,
  52. ul#menu ul li a:active {
  53. width: 8em;
  54. }
  55. Use the following to copy and paste the code.

Of course you will need to change the colors and text sizes to ensure it blends with the rest of the theme.

Continue reading. »

Oh, as an Aside: AJAX’d Wordpress Changelog (From the 16th of November)

AJAXed WordPress version 1.23 has been released. This version introduces French and Spanish support as well as compatibility with WordPress 2.6.

Changes in aWP Version 1.23

Lots of little stuff.

Continue reading. »

Oh, as an Aside: AJAXed WordPress (From the 10th of November)

AJAXed Wordpress (AWP) harnesses the power of both AJAX and Wordpress to improve the user experience, the administration capabilities and the design potential of any Wordpress based blog. It works on all WordPress versions from 2.1 – 2.6.

NEW VERSION AND NEW WEBSITE!

AJAXed WordPress has its own website at ajaxedwp.com. All updates and support are now given there.

DIY Javascript Effects Library

As part of a project I needed an extremely light-weight special effects library that would allow queuing of effects (required), multiple effects, the ability to set a per-effect “framerate”, and enough extensibility to fill any need, but it had to be independent of any Javascript library or OS.

For the “core”: I needed it to be able to queue effects to run one after another, but still be able to run effects synchronously with their own queues. Setting up the effects should take no more than a few lines, and the library should be easy adapted to the website or app it is currently running on. The library must also be cross browser, run well on lower end computers, and very very small (the main reason for creating it was as a replacement in a project for people who didn’t want to use jQuery or Scriptaculous just for the effects.) The core should be able to do everything with only being told the element’s ID, whether we are showing or hiding it (if applicable), and the effect to use.

Continue reading. »

The Online Desktop.

Is anyone using StartForce? It is an online desktop that is built in JavaScript, I played around with it a little bit and was impressed, but at 424KB compressed and 1305KB uncompressed it is definitely not for the lower end computer if you like to do other things on it.

Update:
Sorry, I forgot there was this thing called anchor text in links.