Category Archives: Web Developing

Short Ajax Script

April 13, 2008 by aaron

AJAX - it

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

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

“Post” AJAX Script

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

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.

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

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

Read More ⟶

Custom Category Templates on a Archive or Index page.

April 23, 2008 by aaron
On my home page and in my archives, I use a custom category template to display asides and news articles. This is very easy to do and it only takes a couple seconds of work to create custom category templates in any WordPress theme. The first step is to add the following to your current theme’s index.php loop after the line that looks like <?php while (have_posts()) : the_post(); ?>, but before any other code.
Read More ⟶

Can’t connect to local MySQL server through socket…error.

April 21, 2007 by aaron
I recently moved my /home folder to its own partition, but in doing so, I broke MySQL. The full error I got was: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) To fix this you need to create the file and make sure that MySQL has access to it. (All commands need to be run as root) Create the directory (if it doesn’t already exist). sudo mkdir /var/run/mysqld/ Create the file by “touching” it.
Read More ⟶

DIY Javascript Effects Library

April 12, 2007 by aaron
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.
Read More ⟶

PHP include ladder to find include path.

April 10, 2007 by aaron
I needed a way to be able to include particular files from files that are themselves included. because the working directory was not necessarily the same, so I needed a way to find what the actual include path should be. The following code snippet finds a specific folder or file and returns the ladder (relative path) to get to it. function find_path($loc,$dir=0){ for($i = 0; $i < 10; $i++){ if($dir){ if(is_dir($ladder.$loc)){ return $ladder; break; } }elseif(is_file($ladder.$loc)){ return $ladder; break; } $ladder .=&#039;../&#039;; } } This code is NOT user-input safe: any checks you do should be hard coded otherwise you might as well just give out your passwords.

Short Ajax Script

April 13, 2008 by aaron

AJAX - it

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

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

“Post” AJAX Script

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

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.

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

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

Read More ⟶

Custom Category Templates on a Archive or Index page.

April 23, 2008 by aaron
On my home page and in my archives, I use a custom category template to display asides and news articles. This is very easy to do and it only takes a couple seconds of work to create custom category templates in any WordPress theme. The first step is to add the following to your current theme’s index.php loop after the line that looks like <?php while (have_posts()) : the_post(); ?>, but before any other code.
Read More ⟶

Can’t connect to local MySQL server through socket…error.

April 21, 2007 by aaron
I recently moved my /home folder to its own partition, but in doing so, I broke MySQL. The full error I got was: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) To fix this you need to create the file and make sure that MySQL has access to it. (All commands need to be run as root) Create the directory (if it doesn’t already exist). sudo mkdir /var/run/mysqld/ Create the file by “touching” it.
Read More ⟶

DIY Javascript Effects Library

April 12, 2007 by aaron
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.
Read More ⟶

PHP include ladder to find include path.

April 10, 2007 by aaron
I needed a way to be able to include particular files from files that are themselves included. because the working directory was not necessarily the same, so I needed a way to find what the actual include path should be. The following code snippet finds a specific folder or file and returns the ladder (relative path) to get to it. function find_path($loc,$dir=0){ for($i = 0; $i < 10; $i++){ if($dir){ if(is_dir($ladder.$loc)){ return $ladder; break; } }elseif(is_file($ladder.$loc)){ return $ladder; break; } $ladder .=&#039;../&#039;; } } This code is NOT user-input safe: any checks you do should be hard coded otherwise you might as well just give out your passwords.