Archive for December, 2009
JSON jQuery Plugin
14
I finally got a bit of time and I started playing around with the creation of jQuery plugins and did created a couple of them that I believe all of you will find useful, one of them is a JSON plugin.
As you all know jQuery do not have a JSON encode function. I truly do not know the reason why but to implement it was quite easy -maybe the guys from jQuery thought that it wasn’t really necessary and I agree with them. Most of us use JSON on the server side through PHP or whatever the server tech we use but sometimes, and I repeat, sometimes, we require to develop client applications that by using JSON (thanks David Crockford) we can reduce our server resources and the amount of data transmitted between client and server. But this is a subject that I will treat in the next posts.
Here is the plugin code:
jQuery.JSON = {
useHasOwn : ({}.hasOwnProperty ? true : false),
pad : function(n) {
return n < 10 ? "0" + n : n;
},
m : {
"\b": '\\b',
"\t": '\\t',
"\n": '\\n',
"\f": '\\f',
"\r": '\\r',
'"' : '\\"',
"\\": '\\\\'
},
encodeString : function(s){
if (/["\\\x00-\x1f]/.test(s)) {
return '"' + s.replace(/([\x00-\x1f\\"])/g, function(a, b) {
var c = m[b];
if(c){
return c;
}
c = b.charCodeAt();
return "\\u00" +
Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
}) + '"';
}
return '"' + s + '"';
},
encodeArray : function(o){
var a = ["["], b, i, l = o.length, v;
for (i = 0; i < l; i += 1) {
v = o[i];
switch (typeof v) {
case "undefined":
case "function":
case "unknown":
break;
default:
if (b) {
a.push(',');
}
a.push(v === null ? "null" : this.encode(v));
b = true;
}
}
a.push("]");
return a.join("");
},
encodeDate : function(o){
return '"' + o.getFullYear() + "-" +
pad(o.getMonth() + 1) + "-" +
pad(o.getDate()) + "T" +
pad(o.getHours()) + ":" +
pad(o.getMinutes()) + ":" +
pad(o.getSeconds()) + '"';
},
encode : function(o){
if(typeof o == "undefined" || o === null){
return "null";
}else if(o instanceof Array){
return this.encodeArray(o);
}else if(o instanceof Date){
return this.encodeDate(o);
}else if(typeof o == "string"){
return this.encodeString(o);
}else if(typeof o == "number"){
return isFinite(o) ? String(o) : "null";
}else if(typeof o == "boolean"){
return String(o);
}else {
var self = this;
var a = ["{"], b, i, v;
for (i in o) {
if(!this.useHasOwn || o.hasOwnProperty(i)) {
v = o[i];
switch (typeof v) {
case "undefined":
case "function":
case "unknown":
break;
default:
if(b){
a.push(',');
}
a.push(self.encode(i), ":",
v === null ? "null" : self.encode(v));
b = true;
}
}
}
a.push("}");
return a.join("");
}
},
decode : function(json){
return eval("(" + json + ')');
}
};
How to use
Copy and paste the above code onto a file and name it whatever you like, for the sake of the example we will call it jquery.json.js. And then configure your head section like this:
<!-- jquery library (if you dont have it, then download it--> <script language="javascript" src="jquery.1.3.2.js" ></script> <!-- our plugin file --> <script language="javascript" src="jquery.json.js"></script>
That’s it, now we can call our plugin like this:
// test variables
var obj = {json:'this is a test json property',xml:'this is a test xml property'};
var arr = ['A','B of 2 index array','C','D'];
// encoding an object
var a = $.JSON.encode(obj);
alert('json encoded object:'+a);
// decoding an object
var b = $.JSON.decode(a);
alert('json decoded object property:'+b.json);
// encoding an array
a = $.JSON.encode(arr);
alert('json encoded array:'+a);
// decoding the array
b = $.JSON.decode(a);
alert('json decoded array:'+b[1]);
On future posts we will make use of this plugin to show what we can do with it.
Tweet this!
Ajax Powered Autocomplete Plugin for JQuery.js
3
I am glad to announce the launch of the successfull Ajax Powered Autocomplete for Prototype.js now as a plugin for JQuery. No much to say… if any of you want to have a look at this piece of code you can check it here.
Any feedback is highly appreciated.
Tweet this! How to change Paypal’s front page language
3
I am currently developing an application and I wanted to use PayPal’s checkout method as now it allows us, mere mortals, to use our credit cards without the need of being paypal’s user. But, I had a huge problem, I explain:
My Web project has multiple language support, that means, that a guy can choose the language he wants to see or experience my site, that a guy can be in Australia and be Spanish and choose to view my site in Spanish. You may wonder why I am explaining so much this, well, it seems that the guys from PayPal didn’t have that into account as if you do not tweak the form variables correctly, the front page of your checkout process will be displayed in the ASSUMED language by your location: if you are a Spanish in Australia and clicks the button to checkout in my site, PayPal will display their front page in Australian English, no matter who you are Spanish, Italian, or Japanese -can you imagine if you are in Japan and you are not Japanese?. And what does means? YOU LOOSE A SALE!
So, I look around the Web and found nothing at all. Just bits and pieces here and there, nobody gave me any solution at all just the one given by, even, the guys from PayPal:
Include a hidden field type named ‘lc’ and set it to the language you wish the page to view displayed (ie for Spanish language):
<input type="hidden" value="ES" name="lc" />
But that didn’t work, it just displayed a small dropdown box at the top right corner of the page… I kept searching the web… very frustrated I read an article from a guy that said that it was a problem with direct payments as we do not include the shipping information in it, there was nothing to do about it. So after a bit of studying the variables of PayPal’s Web Integration Manual I decided to give a try to the following set:
- I will tell PayPal not to ask for shipping, there is no shipping
- I will tell PayPal the language I want to use
- I will tell PayPal the character set I use for that language
- I will tell PayPal also the currency code I want to use (This is extra
)
The resulting form was like this and believe me… IT WORKS:
<form action="https://www.paypal.com/cgi-bin/webscr" id="paypalform" name="paypalform" method="post" style="display: block;" class="boxy-content"> <input type="hidden" value="2" id="rm" name="rm"/> <input type="hidden" value="_xclick" id="cmd" name="cmd"/> <input type="hidden" value="YOUR_PAYPAL_ID" id="business" name="business"/> <input type="hidden" value="http://YOUR_SUCCESS_URL_PAGE" id="return" name="return"/> <input type="hidden" value="http://YOUR_CANCEL_URL_PAGE" id="cancel_return" name="cancel_return"/> <input type="hidden" value="http://YOUR_IPN_URL_VALIDATION" id="notify_url" name="notify_url"/> <!-- THESE ARE THE IMPORTANT FIELDS TO KNOW --> <input type="hidden" value="EUR" id="currency_code" name="currency_code"/> <input type="hidden" value="ES" id="lc" name="lc"/> <input type="hidden" value="1" id="no_shipping" name="no_shipping"/> <input type="hidden" value="utf-8" id="charset" name="charset"/> <!-- END OF IMPORTANT FIELDS --> <input type="hidden" value="TEST_ITEM_NAME" id="item_name" name="item_name"/> <input type="hidden" value="TEST_ITEM_NUMBER" id="item_number" name="item_number"/> <input type="hidden" value="1.50" id="amount" name="amount"/> </form>
Tweet this! Javascript Online Compression Tools
0
When a Web project is finished, it is time to think how to make it better, faster… how to improve user’s experience. One of the most important things is javascript code compression in order to speed up page downloads and there are a couple of tools around that allows us to do it.
I just include three references:
- The Online YUI Compressor – by Mike
- The Online YUI Compressor – by Rodolphe Stoclin
- The Online Javascript Minifier - by Vance Lucas
The Online YUI Compressors
These tools allows us to make use of the famous Yahoo Javascript Compression algorithm. Both of the above references does the same results, there is only a major difference between both: whilst the one created by Mike allows you to paste your script or upload a file to compress, the one created by Rodolphe Stoclin only allows us to upload the file to compress.
The Online Javascript Minifier
This tool can reduce our code by using two different algorithms: the JSMin by David Crockford and the Packer method by Dean Edwards.
Which One to Use?
I personally like those algorithms that do not make any use of the eval function and doesn’t change much the code I program, test, and implement. I think that will only depend of the developer choice.
There is an online tool that help us compare the algorithms. If that helps you to decide which one to use: http://compressorrater.thruhere.net/
Tweet this! Ajax Load -Ajax Loading GIF Generator
0
Who of those of you that use ajax on your projects aren’t looking around the web to find the perfect ‘loading please wait’ GIF for your brand new web application or site? I have to confess that I was one of them. I am not a designer, I can program any web application in a matter of days but when it comes to design… puff… It is the reason why I am always crawling the web for ‘inspiration’.
One of those days crawling I found an online tool created by kath called Ajax Load – Ajax Loading GIF Generator. There you don’t need to worry if you know how to design or not, this simple but very useful tool allows you to select the type of design you want for your ajax loading gif, the foreground and background colors and voilá, your ajax loading gif is ready to download.

For those, like me, without a clue about designing there it is, the Ajax Load – Ajax Loading GIF Generator.
Tweet this! Creating Small Urls with PHP
0
Have you ever wanted to create small URLs like in Twitter? This service is provided by bit.ly and it is the one I normally use for my projects. It allows you to shorten any long URL (ie. http://www.ramirezcobos.com = http://bit.ly/8zivPq -click on it and you will be redirected here) and also gives you click statistiques.
Nevertheless, there are also other providers that allows you to create a small URL on the fly such as tinyurl.com or to.y. If any of you wishes to create small urls through their PHP code here I got a set of functions (using CURL) for you use on your projects:
function getTinyUrl($url){
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,'http://tinyurl.com/api-create.php?url='.$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
function getToLyUrl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://to.ly/api.php?longurl='.urlencode($url));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec ($ch);
curl_close ($ch);
return $data;
}
Usage
$smallURL = getTinyUrl('http://www.ramirezcobos.com');
echo $smallURL; // will display the small URL
Extras
I include the following function too just in case, as I recommend you to check if URLs are in correct format before using the above functions.
function getParsedUrl($url)
{
$parsed_url = parse_url($url);
if (!isset($parsed_url['scheme']) or $parsed_url['scheme'] != 'http'){
echo 'Unsupported URL sheme given, please just use "HTTP".';
exit();
}
if (!isset($parsed_url['host']) or $parsed_url['host'] == ''){
echo 'Invalid URL given!';
exit();
}
$host = $parsed_url['host'];
$host .= (isset($parsed_url['port']) and !empty($parsed_url['port'])) ? ':'.$parsed_url['port'] : '';
$path = (isset($parsed_url['path']) and !empty($parsed_url['path'])) ? $parsed_url['path'] : '/';
$path .= (isset($parsed_url['query']) and !empty($parsed_url['query'])) ? '?'.$parsed_url['query'] : '';
return 'http://' . $host . $path;
}
Tweet this! Stripe Generator 2.0
0
The Web 2.0 Stripe Generator is one of those great utilities that some good fellows program for us to use freely. This fantastic tool allow us to create, guess what… yeah, stripes!
If you are new to Web 2.0 nowadays design you may wonder why do you need such tool. Well, sometimes we wish to provide our borring form objects some nice background.
Its interface is self explanatory and very easy to use. What I like the most from this tool is its simplicity.
How to use stripes in our CSS Objects
This is even easier, let’s imagine whe are on Stripe Generator:
- Play with sliders and color pickers, untill you obtain a super-cool stripe tile
- Press “Download” to save your creation
- Edit your css adding this line to the element you want to stripe:
background-image: url("path-to-stripe.png"); - If you want to only repeat horizontally your tile (as in the case of tiles with gradient), you must add this line too:
background-repeat: repeat-x;
Example
The following is a picture taken from the contact section of www.antcut.com. I used the above instructions to set the input fields of my document.

Tweet this! Image Gallery via Ajax using JQuery Tools
8
The other day I was having a bit of headache trying to find the best way to display a gallery via Ajax for one of my projects. There are tons of great lightbox type scripts out there (I even have one: lightboxXL) but none of them truly suit my needs as most of them, including mine, are creating the image galleries on DOM load, or on Document load, or on Window load… none of them allowed me to make use of Ajax and I certainly didn’t want to rewrite the code of anybody.
Suddenly, searching the web I found the OVERLAY object from JQuery Tools. This library offers a set of objects that extend the functionality of JQuery, it contains six of the most useful JavaScript tools available for today’s website. The beauty of this library is that all of these tools can be used together, extended, configured and styled. In the end, you can have hundreds of different widgets and new personal ways of using the library.
This is how I used it to create the Image Gallery via Ajax:
Insertion of Libraries
First we must include the library on the HEAD section of the page (JQuery Tools have a compressed version of its tools and latest version of JQuery library)
<!-- Full version of jQuery Tools + jQuery 1.3.2 --> <script type="text/javascript" src="js/jquery.tools.min.js"></script>
CSS Coding
This is where we are going to apply styles to our overlay.
/* scrollable should not disable gallery navigation */
#gallery .disabled {
visibility:visible !important;
}
#gallery .inactive {
visibility:hidden !important;
}
/* the overlayed element */
.simple_overlay {
/* must be initially hidden */
display:none;
/* place overlay on top of other elements */
z-index:10000;
/* styling */
background-color:#333;
width:675px;
min-height:200px;
border:1px solid #666;
/* CSS3 styling for latest browsers */
-moz-box-shadow:0 0 90px 5px #000;
-webkit-box-shadow: 0 0 90px #000;
}
/* close button positioned on upper right corner */
.simple_overlay .close {
background-image:url(images/overlay/close.png);
position:absolute;
right:-15px;
top:-15px;
cursor:pointer;
height:35px;
width:35px;
}
/* styling for elements inside overlay */
.simple_overlay .details {
position:absolute;
top:15px;
right:15px;
font-size:11px;
color:#fff;
width:150px;
}
.simple_overlay .details h3 {
color:#aba;
font-size:15px;
margin:0 0 -10px 0;
}
/* "next image" and "prev image" links */
.next, .prev {
/* absolute positioning relative to the overlay */
position:absolute;
top:40%;
border:1px solid #666;
cursor:pointer;
display:block;
padding:10px 20px;
color:#fff;
font-size:11px;
/* upcoming CSS3 features */
-moz-border-radius:5px;
-webkit-border-radius:5px;
}
.prev {
left:0;
border-left:0;
-moz-border-radius-topleft:0;
-moz-border-radius-bottomleft:0;
-webkit-border-bottom-left-radius:0;
-webkit-border-top-left-radius:0;
}
.next {
right:0;
border-right:0;
-moz-border-radius-topright:0;
-moz-border-radius-bottomright:0;
-webkit-border-bottom-right-radius:0;
-webkit-border-top-right-radius:0;
}
.next:hover, .prev:hover {
text-decoration:underline;
background-color:#000;
}
/* when there is no next or previous link available this class is added */
.disabled {
visibility:hidden;
}
/* the "information box" */
.info {
position:absolute;
bottom:0;
left:0;
padding:10px 15px;
color:#fff;
font-size:11px;
border-top:1px solid #666;
}
.info strong {
display:block;
}
/* progress indicator (animated gif). should be initially hidden */
.progress {
position:absolute;
top:45%;
left:50%;
display:none;
}
/* everybody should know about RGBA colors. */
.next, .prev, .info {
background:#333 !important;
background:rgba(0, 0, 0, 0.6) url(images/h80.png) repeat-x;
}
HTML Coding
Now the HTML holder
<!-- overlay element --> <div class="simple_overlay" id="gallery"> <!-- "previous image" action --> <a class="prev">prev</a> <!-- "next image" action --> <a class="next">next</a> <!-- image information --> <div class="info"></div> <!-- load indicator (animated gif) --> <img class="progress" src="images/ajax-loader.gif" /> <!-- end of overlay element --> </div>
PHP Response
By now, you should already know how to do Ajax calls using JQuery, if you don’t please review it on JQuery’s site. What we are going to check is how is the PHP response in order to make our Image Gallery work after an Ajax call.
// ************************************
// Scenario: An ajax call to this PHP script was done and
// a POST variable has been sent in order to fill $pics from
// a database.
//
// pics == array of pictures
// ************************************
// loop the array
foreach($pics as $pic)
{
// check the class attribute --> ibox
// $pic is also an object with certain properties in this case
$html .= '<div class="pic">
<a href="'.$pic->imgPath.$pic->strName.'" class="ibox" title="'.$pic->strName.'">
<img src="'.$pic->imgPath.$pic->strName.'" width="50" border="0" /></a>
</div>';
}
//
// here I create the necessary javascript code to load the OVERLAY object of JQuery Tools
// please check the 'ibox' call
//
$html .= '
<script type="text/javascript">
//<![CDATA[
$('.ibox').overlay({
target: '#gallery',
expose: '#f1f1f1'
}).gallery({
speed: 800 });
//]]></script>';
//
// echo response
//
echo $html;
And that’s it, once your image gallery is loaded whereever you wish to from an Ajax call, the latest javascript will fire and create the Image Gallery.
Addendum
I don’t want to say that there aren’t other ways to do exactly what I did in this post. I just share what was, in my case, the solution that I implemented into my project and it worked. I will go deeper into JQuery Tools in future posts. Nevertheless, hope you find this post useful.
Tweet this! DimDim Internet Video Conferencing
0
DimDim is a Web utility that lets anyone deliver synchronized live presentations, whiteboards and webpages and share their voice and video over the Internet without any download requirements. In short, it is an online Web Conference Tool. If you ever wished to realize a conference that offers more than just video streaming, then DimDim is a good alternative, and cheaper than other systems like Webex.
It has three versions:
- The free one (with a limitation of 20 people -did you ever had a web conference over such number?)
- The payed one, version PRO (that allows us to have a Web conference with over 50 people and access to stats, separated and faster servers and better security).
- And Webminar (up to 1000 participants and with a monthly cost of 75 USD)
Check it out, is worth to see it.
Tweet this! EzCryptoApi OCX Control
6
Bit of History
Back in year 2001, I was working as a Visual Basic Developer. The reason why I choosed that programming language was obvious, the first book about programming landed in my hands when I was 27 years old, and the easiest programming language to learn in order to work on the field ‘fast’ was for me Visual Basic.
Everybody was talking really bad about Visual Basic, saying that it was NOT a programming language, its slow program execution rates, and so on… I thought they were wrong (somehow) and worked really hard in order to speed up every single process of Visual Basic and the only way to do it was by using external DLL libraries written in C or C++. The only challenge was that Dynamic Libraries where really hard to use, they could break at anytime the program if not well programmed, or just they didn’t do nothing at all.
This is how I started programming a set of Activex Controls that VB programmers could use and easily implement into their programs. The first control was EzCryptoAPI… and you may wonder:
What is EzCryptoAPI?
“EzCryptoApi Control makes use of two Cryptographic Service Providers: “Microsoft Base Cryptographic Provider v.1″ and “Microsoft Enhanced Cryptographic Provider”. By default, Windows 9X and Windows 2000 comes with “Microsoft Base Cryptographic Provider v.1″, which will allow EzCryptoApi to make use of the following encryption algorithms: RC2, RC4, and DES. EzCryptoApi will not be able to use Triple-DES or Triple-DES-112 encryption algorithms if “Microsoft Enhanced Cryptographic Provider” is not installed on the computer.”
EzCryptoAPI on the Web
This ActiveX Control has been downloaded more than 19000 times already, it was submitted to the good old www.planetsourcecode.com. They are tons of VB Programs using this little piece of code, I do not know if they still VB5 programmers around but if you are curious, make a search on GOOGLE.
DOWNLOAD
Tweet this!
EzCryptoAPI OCX Control