Ian Tearle

is working hard, but missing Rachael so much takes its toll!

Updated 1 day, 12 hours, 40 minutes ago

Javascript Goodness

1

jQuery Hide / Show Bug

by Ian Tearle 1:41pm Tuesday, Apr 29

comment | 0 so far

Firstly a big thank you to the original poster of this bug, Jamie Thomspon thank you very much, although it took me a while to find it, thus I am posting it here for reference use.

The issue being is that Safari has trouble with hide/show methods on elements when used by jQuery. The same bug has been reported about fadeIn/fadeOut methods also, although I have not experienced this.

The issue can be solved by the use of CSS. By applying CSS rules to the element, Safari appears to control the hide/show methods correctly.

// This fails in Safari
$(element).hide();
// Change it to this
$(element).css('display','none');

Javascript IE Stumbles!

by Ian Tearle 3:04pm Thursday, Jan 24

comment | 0 so far

I thought I may post this little hint at the woes of Internet Explorer.

Whilst creating an application laden with Javascript, testing with Firefox, and the wonder that is Firebug, all seems well.
But then comes the dreaded IE game. For some reason my Javascript failed to do what it was supposed to do. That some reason ended up being a COMMA. yes my friends, a ,! trailing at the end of a function IE gives up when it thinks the script carries on and starts looking for the rest of the code, when it cant find it, IE will give up on Javascript all together.

So next time you are pulling your hair out, just check for those commas with nothing proceeding it. That will be what Internet Explorer is moaning about, pretty much guaranteed.

JQuery AutoSuggest

by Ian Tearle 12:11pm Wednesday, Jan 16

comment | 0 so far

During the process of developing an application for work the other day, I decided that a normal Javascript enabled interactive menu wasn't enough for what I wanted. The hunt for an attractive auto suggestion, quick search tool was on.

After many searches and deliberations my focus was set on the JQuery AutoSuggest script from BrandSpankingNew. The first issue I discovered was the PHP and ability to generate the correct array for the autosuggest to read from. The vast amount of questions on the website all point to one thing, no-one knows, or no-one wanted to share how to correctly read and loop from MySQL and create the array in PHP for autosuggest.

Well I am pleased to say, I did it. As comments are closed on BSN I would like to leave this on my web site for everyone searching.
Here goes.

Firstly with test.php (how autosuggest ships) add your connection details to MySQL database. Simple stuff if your used to PHP, if not try this:

$dbhost = 'host';
$dbuser = 'user';
$dbpassword = 'password';
$mysqlLink = mysql_connect($dbhost,$dbuser,$dbpassword);
global $mysqlLink;
mysql_select_db('yourDatabase',$mysqlLink) or die(header("Location: message.php" ));
$query = "SELECT * FROM `table` LIMIT 5 LIKE %'entry'";
$result = mysql_query($query);

Next you need to blank your ARRAY, default it to 0 if you like, this is how you would do it.

$aUsers = Array();
$aInfo = Array();
$aId = Array();
$aSid = Array();

You will see I have added another ID called SID, this could be anything else you wanted to pull back from the database and use later within the results.

Next create your ARRAY, here we need to loop the results and build the array in the format autosuggest is looking for.

$row = "";
while($row = mysql_fetch_array($result))
{
        $aId[] = $row["id"];
        $aSid[] = $row["subjectYear"];
        $aUsers[] = $row["subjectName"];
        $aInfo[] = $row['courseLevel'];
}

As you can see we again we have to reset $row to 0, created a while loop with our MySQL connection results, created earlier, and built an array associated with our variables that autosuggest requires.
Thats all you need to do to fetch variable database driven data for autosuggest to suggest from!
But wait, what if you wanted to pass more information to the generated HTML, or form that you are using, so that when you click submit it passes information along the query string to go to a variable page or other use. Well, you will need to edit the line controlled via the variable $aResults.

$aResults[] = array(
     "id"=>($aId[$i]),
    "sid"=>($aSid[$i]) ,
    "value"=>htmlspecialchars($aUsers[$i]),
    "info"=>($aInfo[$i]) );

As you can see, I have added my extra id $aSid into the equation. This will build the array string for JSON with all the information I need to created a query sting!
Next you need to make sure that this information is inserted via the javascript into the correct position, to do this, edit the autosuggest.js around line 315 and simply add you variables:

this.aSug.push(  { 'id':jsondata.results[i].id, 'sid':jsondata.results[i].sid, 'value':jsondata.results[i].value, 'info':jsondata.results[i].info }  );

Do you see my SID? Next the callback: function within the options script held on the HTML page, just add in your variable data, I wanted to add in the SID to a hidden field on my form, so that the query string would pass two id's along the URL to open a dynamically generated page.

callback: function (obj) { document.getElementById('pid').value = obj.id, document.getElementById('sid').value = obj.sid, document.qSearch.submit();}

I also added this line: document.qSearch.submit(); when the user clicks on one of the results, the form gets submitted and jumps to the correct page, much like the quick search on Facebook.

Lesson over. Hope that you found this useful? Let me know if you have any other suggestions, and thanks would be kindly accepted on this blog!

JQuery Against Anything Else.

by Ian Tearle 11:27pm Sunday, Apr 15

comment | 0 so far

I have fallen in love with a new Javascript Library. Gone are the days of prototype, I have found JQuery, which I have quickly learnt and begun to write my own custom variables.

OK so Prototype has the experience, the big guns behind one of the most popular Javascript Libraries around, but JQuery is so simple, you don't have to borrow someone else's good looking code, you can make your own. There is a growing library on the web too, with many good developers sharing their skills, like those over at Malsup with clever examples and the briefest but most helpful of code, they have enabled me to play with JQuery and make it work for me. My version Three of Media By will be heavy on JQuery, making use of the graphical interface rather than the technology, all the while making the site look GOOD.

The fear though; 'is javascript a wise choice for web sites?' My answer is why not? So many browsers have got their act together with regards to security, and more and more people are becoming aware of web 2.0, you just have to put CSS in your browser to see the abundant list of web 2.0 galleries, and standard compliant sites. Javascript libraries are for the future. They are compatible with everything, they render well even in IE, and they are accessible.

So spread the word, turn on your javascript, the future is now. Lets make the web the future, and make it look good.