The Urbano A. Company’s Blog
Javascript
Articles about JavaScript development and library oriented scripting
Perfect jQuery UI rotating tabs
Oct 20th
Tabs are nice. They create a very elegant interface, and jQuery UI does this marvelously, here we have a little preview of this:

jQuery UI Tabs
How to do that?
Include the files
With jQuery and jQuery UI it is dead simple. First, load the JS libraries, I recommend using the hosted files at Google for jQuery:
<script src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js" type="text/javascript"></script> <script src="path/to/jquery UI" type="text/javascript"></script>
Remember to include also the CSS files for the UI Theme! Otherwise the tabs won't seem to work!
The HTML for the tabs:
We need a little HTML for our tabs to work:
A wrapper, a few divs with the content, and an unordered list with the tabs:
<div id="tabs-rotate"> <ul> <li><a href="#tab1"><span>Tab 1</span></a></li> <li><a href="#tab2"><span>Tab 2</span></a></li> <li><a href="#tab3"><span>Tab 3</span></a></li> </ul> <div id="tab1">Tab 1 content</div> <div id="tab2">Tab 2 content</div> <div id="tab3">Tab 3 content</div> </div>
The JavaScript:
Now comes the cool part, basically we want to have tabs. But although tabs are nice, people may not realize there is more content, or they might be just too lazy to browse through it, so, why not rotate through the tabs?
jQuery UI does that by itself with a very simple commant, but it is not perfect! So we need to program our tabs to rotate, unless the mouse is over them. This way when a user is looking for a link, the tab will wait!
So here is how this is achieved:
$(document).ready(function(){ $("#tabs-rotate").tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 3000); $('#tabs-rotate').hover(function(){ $(this).tabs('rotate', 0, false); },function(){ $(this).tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 3000); } ); });
First we instruct jQuery UI to set up tabs in that div, and to rotate them. Then we use a hover event to control the rotation. On hover no rotation, and on out resume rotation.
See it in action
You can see it working on the DJs Music homepage
As with the best stuff, it's terribly simple
Hope you enjoy,
Alex
Easy JavaScript Matrix
Sep 6th
A very cool way to display stuff on a website, is using a Matrix style. It is also quite common amongst hackers, and most of the people who have hacked a site for the sole purpose of proving they can like to display their hacker name in the index file, sometimes using JavaScript matrix style.
Well, here a way to do it:
All you have to do is copy and paste the following javascript code into your website:
var rows=21; var speed=3; var reveal=3; var effectalign="default"; var w3c=document.getElementById && !window.opera;; var ie45=document.all && !window.opera; var ma_tab, matemp, ma_bod, ma_row, x, y, columns, ma_txt, ma_cho; var m_coch=new Array(); var m_copo=new Array(); window.onload=function() { if (!w3c && !ie45) return var matrix=(w3c)?document.getElementById("matrix"):document.all["matrix"]; ma_txt=(w3c)?matrix.firstChild.nodeValue:matrix.innerHTML; ma_txt=" "+ma_txt+" "; columns=ma_txt.length; if (w3c) { while (matrix.childNodes.length) matrix.removeChild(matrix.childNodes[0]); ma_tab=document.createElement("table"); ma_tab.setAttribute("border", 0); ma_tab.setAttribute("align", effectalign); ma_tab.style.backgroundColor="#000000"; ma_bod=document.createElement("tbody"); for (x=0; x<rows ; x++) { ma_row=document.createElement("tr"); for (y=0; y <columns; y++) { matemp=document.createElement("td"); matemp.setAttribute("id", "Mx"+x+"y"+y); matemp.className="matrix"; matemp.appendChild(document.createTextNode(String.fromCharCode(160))); ma_row.appendChild(matemp); } ma_bod.appendChild(ma_row); } ma_tab.appendChild(ma_bod); matrix.appendChild(ma_tab); } else { ma_tab='<ta'+'ble align="'+effectalign+'" border="0" style="background-color:#000000">'; for (var x=0; x</rows><rows ; x++) { ma_tab+='<t'+'r>'; for (var y=0; y <columns ; y++) { ma_tab+='<t'+'d class="matrix" id="Mx'+x+'y'+y+'"> '; } ma_tab+=''; } ma_tab+=''; matrix.innerHTML=ma_tab; } ma_cho=ma_txt; for (x=0; x</columns> <columns ; x++) { ma_cho+=String.fromCharCode(32+Math.floor(Math.random()*94)); m_copo[x]=0; } ma_bod=setInterval("mytricks()", speed); } function mytricks() { x=0; for (y=0; y<columns; y++) { x=x+(m_copo[y]==100); ma_row=m_copo[y]%100; if (ma_row && m_copo[y]<100) { if (ma_row<rows+1) { if (w3c) { matemp=document.getElementById("Mx"+(ma_row-1)+"y"+y); matemp.firstChild.nodeValue=m_coch[y]; } else { matemp=document.all["Mx"+(ma_row-1)+"y"+y]; matemp.innerHTML=m_coch[y]; } matemp.style.color="#33ff66"; matemp.style.fontWeight="bold"; } if (ma_row>1 && ma_row<rows +2) { matemp=(w3c)?document.getElementById("Mx"+(ma_row-2)+"y"+y):document.all["Mx"+(ma_row-2)+"y"+y]; matemp.style.fontWeight="normal"; matemp.style.color="#00ff00"; } if (ma_row>2) { matemp=(w3c)?document.getElementById("Mx"+(ma_row-3)+"y"+y):document.all["Mx"+(ma_row-3)+"y"+y]; matemp.style.color="#009900"; } if (ma_row <math .floor(rows/2)+1) m_copo[y]++; else if (ma_row==Math.floor(rows/2)+1 && m_coch[y]==ma_txt.charAt(y)) zoomer(y); else if (ma_row<rows+2) m_copo[y]++; else if (m_copo[y]<100) m_copo[y]=0; } else if (Math.random()>0.9 && m_copo[y]<100) { m_coch[y]=ma_cho.charAt(Math.floor(Math.random()*ma_cho.length)); m_copo[y]++; } } if (x==columns) clearInterval(ma_bod); } function zoomer(ycol) { var mtmp, mtem, ytmp; if (m_copo[ycol]==Math.floor(rows/2)+1) { for (ytmp=0; ytmp<rows ; ytmp++) { if (w3c) { mtmp=document.getElementById("Mx"+ytmp+"y"+ycol); mtmp.firstChild.nodeValue=m_coch[ycol]; } else { mtmp=document.all["Mx"+ytmp+"y"+ycol]; mtmp.innerHTML=m_coch[ycol]; } mtmp.style.color="#33ff66"; mtmp.style.fontWeight="bold"; } if (Math.random()<reveal) { mtmp=ma_cho.indexOf(ma_txt.charAt(ycol)); ma_cho=ma_cho.substring(0, mtmp)+ma_cho.substring(mtmp+1, ma_cho.length); } if (Math.random()<reveal-1) ma_cho=ma_cho.substring(0, ma_cho.length-1); m_copo[ycol]+=199; setTimeout("zoomer("+ycol+")", speed); } else if (m_copo[ycol]>200) { if (w3c) { mtmp=document.getElementById("Mx"+(m_copo[ycol]-201)+"y"+ycol); mtem=document.getElementById("Mx"+(200+rows-m_copo[ycol]--)+"y"+ycol); } else { mtmp=document.all["Mx"+(m_copo[ycol]-201)+"y"+ycol]; mtem=document.all["Mx"+(200+rows-m_copo[ycol]--)+"y"+ycol]; } mtmp.style.fontWeight="normal"; mtem.style.fontWeight="normal"; setTimeout("zoomer("+ycol+")", speed); } else if (m_copo[ycol]==200) m_copo[ycol]=100+Math.floor(rows/2); if (m_copo[ycol]>100 && m_copo[ycol]<200) { if (w3c) { mtmp=document.getElementById("Mx"+(m_copo[ycol]-101)+"y"+ycol); mtmp.firstChild.nodeValue=String.fromCharCode(160); mtem=document.getElementById("Mx"+(100+rows-m_copo[ycol]--)+"y"+ycol); mtem.firstChild.nodeValue=String.fromCharCode(160); } else { mtmp=document.all["Mx"+(m_copo[ycol]-101)+"y"+ycol]; mtmp.innerHTML=String.fromCharCode(160); mtem=document.all["Mx"+(100+rows-m_copo[ycol]--)+"y"+ycol]; mtem.innerHTML=String.fromCharCode(160); } setTimeout("zoomer("+ycol+")", speed); } } </rows></math> </rows></columns></rows>
Now in the page, place a div with id matrix and whatever text you want to see appear...
For example:
<div id="matrix">Urbano's Blog</div>
There are a lot of variables to play with, like speed, timing, columns, rows... But the basics are there... Hope you find this interesting
Google AJAX search API
Jun 1st
Discover a great way to embed really customized searches in your site, use the Google search API (Using AJAX)
Here I'll show you how to develop a simple "Hello world" sort of program, using the Google API and AJAX to search.
There are 2 requirements though you need to fulfill in order to access the API:
- Your web site must be freely accessible to end users.
- Google will upgrade this API periodically, and you must update your site to use new versions of the API as they become available. The Gogle AJAX Search API team will post notifications of updates on the Google AJAX Search API Blog. (http://googleajaxsearchapi.blogspot.com/).
The second one is not that much of a requirement, but something you must take care of for your search to work properly...
So apply for your API key, and take into account that a single AJAX Search API key is valid within a single directory on your web server, including any subdirectories.
More information on the subject
So let's get going:
This is the HTML code of a website running the AJAX search:
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title>Hello World - Google AJAX Search API Sample</title> <link href="http://www.google.com/uds/css/gsearch.css" type="text/css" rel="stylesheet"/> <script src="http://www.google.com/uds/api?file=uds.js&v=1.0" type="text/javascript"></script> <script language="Javascript" type="text/javascript"> //< ![CDATA[ function OnLoad() { // Create a search control var searchControl = new GSearchControl(); // Add in a full set of searchers var localSearch = new GlocalSearch(); searchControl.addSearcher(localSearch); searchControl.addSearcher(new GwebSearch()); searchControl.addSearcher(new GvideoSearch()); searchControl.addSearcher(new GblogSearch()); searchControl.addSearcher(new GnewsSearch()); searchControl.addSearcher(new GimageSearch()); searchControl.addSearcher(new GbookSearch()); // Set the Local Search center point localSearch.setCenterPoint("New York, NY"); // tell the searcher to draw itself and tell it where to attach searchControl.draw(document.getElementById("searchcontrol")); // execute an inital search searchControl.execute("Urbano's Blog"); } GSearch.setOnLoadCallback(OnLoad); //]]> </script> </head> <body> <div id="searchcontrol">Loading</div> </body> </html>
So that is a simple page using the search, I'll now go through the code explaining it:
First of all, we import Google's CSS style sheet from its location:
http://www.google.com/uds/css/gsearch.css
Next we import the Google AJAX search library from its location:
http://www.google.com/uds/api?file=uds.js&v=1.0
And now with the search controls:
For the configuration we will create a function, in this case OnLoad(). This function sets up the controls for our search, and draws the searcher inside the specified div.
To start, we need a new search control, which we set up as follows:
var searchControl = new GSearchControl();
So we have initialized the searcher, now define which areas we want to search in, in this case most of them:
searchControl.addSearcher(localSearch);
searchControl.addSearcher(new GwebSearch());
searchControl.addSearcher(new GvideoSearch());
searchControl.addSearcher(new GblogSearch());
searchControl.addSearcher(new GnewsSearch());
searchControl.addSearcher(new GimageSearch());
searchControl.addSearcher(new GbookSearch());
We are using local, web, video, blog, news, image, and book searchers.
For the local search to work you need to set a "center point". For it to work perfectly you could use php for example and geoip to detect the location of the user and use that as center point... In the example we'll be using NY:
localSearch.setCenterPoint("New York, NY");
Now display the searcher in the specified div (This could be any div in your site, simply reference it and it will draw the searcher inside it). In this case "searchcontrol":
searchControl.draw(document.getElementById("searchcontrol"));
This is optional, but if you want to see it working you can set up an initial search. If you want this to work from your own search text fields, use GET or POST vars and php to set up the script to search for a given variable...
In this case we will be searching for "Urbano's Blog":
searchControl.execute("Urbano's Blog");
Be careful with the above to always strip ", because it would brake your script!
And you are basically done, simply call the OnLoad function and you are good to go!
GSearch.setOnLoadCallback(OnLoad);
Hope you enjoyed it!
Read more about this:
- Developer Documentation @ Google Code
- Class and function reference @ Google Code
