The smallest AJAX framework ever July 21st, 2009
The most AJAX frameworks are bloated with many functions you’ll never use or need. Who wants 60 KB of JavaScript if your only need is to send and load data? I Don’t.
I have found somewhere a short howto for AJAX, and derived (rewritten) the smallest AJAX framework ever out of it:
var ajax = false;
if(window.XMLHttpRequest) ajax = new XMLHttpRequest();
else if(window.ActiveXObject) {
try { ajax = new ActiveXObject("Msxml2.XMLHTTP"); }
catch (e) {
try { ajax = new ActiveXObject("Microsoft.XMLHTTP"); }
catch (e) {}
}
}
function ajaxGetContent(type, url, parameter, output_function) {
if(!ajax) eval(output_function + '(false);');
ajax.onreadystatechange = function() {
if(output_function != 'void' && ajax.readyState == 4) {
if(ajax.status == 200) eval(output_function + '(ajax.responseText);');
else eval(output_function + '(false);');
}
}
if(type == "post") {
ajax.open('POST', url, true);
ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
ajax.send(parameter);
} else {
ajax.open('GET', url + ((parameter.length > 0)?"?":"") + parameter, true);
ajax.send(null);
}
}
That’s short, isn’t it?
To get the data you want you have to simply call the function and define a function which will be called after the data arrives:
ajaxGetContent('post', '/myURL/index.php', 'a=b', 'myDataCallBack');
function myDataCallBack(myContent) {
if(myContent == false) {
// something went wrong
} else {
// have fun with your data
}
}
And if you want to send data only and don’t care of the answer, use this syntax:
ajaxGetContent('post', '/myURL/index.php', 'a=b', 'void');
Some explanation:
- type = How to send the data (Parameters). “post” or “get”.
- url = Where to send it to.
- parameter = URL-style parameters. Never use a question mark!
- output_function = The function which will be called after the data is received.
This Tool is tested in Opera 9+10, Firefox 2+3 and MSIE 7+8.
Have fun!
Posted in Programming, Tools | No Comments »
MySQL Birthday Column – Age of a Person July 8th, 2009
If you search for a solution to get the age of a person (in years) in MySQL, google gives you many obsolete (and complicated) answers, from MySQL 4.1.1 it is very easy to get this number:
TIMESTAMPDIFF(YEAR, myDateColumn, NOW())
Have fun.
DIV layer with 100% height, the Javascript way July 1st, 2009
When you create a div layer with 100% width and height, the height is always only the height of the browser window, not of the page content (which can be much higher).
To solve this, give the layer div an id and use the following JS code inside the layer div:
<script type="text/javascript">document.getElementById("yourlayerid").style.height = document.getElementsByTagName("body")[0].offsetHeight + "px";</script>
The code is very simple, it gets the height of the content (the body element) and gives it the layer div.
Tested in Opera, Firefox and *barf* MSIE (8).
Posted in Programming | No Comments »
How to force file downloading with htaccess July 1st, 2009
Nearly every browser will show image oder video files you want to provide for download in the browser itself instead of opening a download dialog.
Most of the websites suggest to use this code in your .htaccess:
<FilesMatch "\.(jpg|zip|avi)$" >
ForceType application/octet-stream
</FilesMatch>
But this is not enough! Even when the MIME-Type is set to “octet-stream”, some browsers will still open the files their selfs because they have detected the .jpg or .avi file extension.
The solution to this is to add the following header:
<FilesMatch "\.(jpg|zip|avi)$" >
ForceType application/octet-stream
Header add Content-Disposition "attachment"
</FilesMatch>
With this header every browser i tested opened a download dialog, regardless of which file extension is present.