juillet 2007 - jf-hovinne.blog

lundi 30 juillet 2007

fixedBox jQuery plugin

fixedBox is a lightweight jQuery plugin that allows you to display a fixed box in the web page.
The box is centered by default, but you can also define the left and/or top positions.

Look at a fixedBox example.

Usage:

centered:
$('div.message').fixedBox();

left = 200px, top = 100px:
$('div.message').fixedBox({x: 200, y: 100});

Note: you'll need to set the width and height of the matched nodes (e.g. in your CSS, see the styles in the provided example), in order to correctly center them.

Tested with jQuery 1.1.3.1, Firefox 1.5+, Opera 9.0+, MSIE 6.0+. Should also work with Safari 2.0+.

Download fixedBox.
Download fixedBox packed.

License: MIT/GPL.

lundi 16 juillet 2007

JSS: JavaScript Simple Syndication - yet another implementation

jFeed, the jQuery RSS/ATOM feed parser plugin I wrote lately has a drawback: you must use a server-side proxy to load cross-domain feeds.

It's a little bit annoying, so I looked for a more elegant solution.
There exist some web services and libraries that convert RSS/ATOM feeds to JavaScript code, so you can embed external content in your HTML document, but I'd prefer something more generic.

Another JSS proposal is to convert XML data (e.g. RSS) to JSON at the server-side, and to dynamically load it as a JS script at the client side.

There's a XSLT to do the conversion from XML to JSON, XSLTJSON, but unfortunately it uses XSLT 2.0, which isn't very wide-spread at the moment.

So I searched for another solution and got the best results with the BadgerFish convention, implemented (among others) in PHP.

I wrote a simple PHP class to use it:

<?php
require_once('BadgerFish.php');
class JSS {
   public static function encode($url, $callback) {
       $doc = new DOMDocument;
       $doc->load($url);
       $out = BadgerFish::encode($doc);
       return($callback . '(' . $out . ');');
   }
}
?>

JSS::encode loads the local or external XML document, applies the BadgerFish transformation, and returns the JSON data as the callback function argument, in the form:

callback({...JSON data...});

Note: the external JS script will be loaded asynchronously, thus we need a callback function.

This is the actual PHP file which will output the data:

<?php
require_once('jss/JSS.php');
$callback = $_REQUEST['callback'];
$url = 'rss.xml';
header("Content-Type: application/x-javascript");
echo(JSS::encode($url, $callback));
?>

And now the client-side JavaScript part:

var JSS = {
   counter: -1,
   getFeed: function(url, callback) {
   
       this.counter ++;
       JSS[this.counter] = callback;
       var body = document.getElementsByTagName('body')[0];
       var script = document.createElement('script');
       script.type = 'text/javascript';
       script.src = url + '?callback=JSS[' + this.counter + ']';
       body.appendChild(script);
   }
};

We append the external script in the document's BODY, passing the callback function name in the query, in the form:

<script type="text/javascript" src="...url...?callback=JSS[counter]"></script>

And finally, the HTML document which will import the JSON data and execute the callback:

<!DOCTYPE ...
<html>
<head>
...
<script type="text/javascript">
function initJss() {
   JSS.getFeed('http://www.hovinne.net/feed/jss.php', function(data) {
       console.log(data); //use Firebug console
   });
};
</script>
</head>
<body onload="initJss();">
...
</body>
</html>

A JSS implementation example is available, as well as the JSS source code, licensed under MIT license.

Next step: parse the JSON feeds with jFeed!

dimanche 15 juillet 2007

jFeed: JavaScript jQuery RSS/ATOM feed parser plugin

I needed a lightweight JavaScript feed parser based on jQuery and couldn't find one, so I wrote jFeed yesterday.
Thanks to jQuery, it was quite easy and fun to do.

jFeed currently parses RSS 0.91, 0.92, 1.0, 2.0 and Atom 1.0 feeds.

Usage:

jQuery.getFeed(options);
   
   options:
   
   * url: the feed URL (required).
   * data: data to be sent to the server. See jQuery.ajax data property.
   * success: a function to be called if the request succeeds.
     The function gets passed one argument: the JFeed object.
   
   Example:
   
   jQuery.getFeed({
       url: 'rss.xml',
       success: function(feed) {
           alert(feed.title);
       }
   });

JFeed properties:

   * feed.type
   * feed.version
   * feed.title
   * feed.link
   * feed.description
   * feed.language
   * feed.updated
   * feed.items: an array of JFeedItem

JFeedItem properties:

   * item.title
   * item.link
   * item.description
   * item.updated
   * item.id

jFeed is dual licensed under MIT/GPL.
Download jFeed

Examples are provided in the archive, as well as a basic PHP proxy (testing purposes only) for loading external feeds.

Note: the feed must be sent as XML, thus the content-type needs to be specified as text/xml or application/xml, see Specifying the Data Type for AJAX Requests.

jeudi 12 juillet 2007

jQuery charToTable plugin

I was playing with jQuery and tables yesterday, and got a weird idea: what about converting characters to tables using jQuery?

The idea evolved and I started to write a new jQuery plugin, charToTable.

I really don't know if someone will find it useful, but it actually works fine.
It uses some PHP + GD to convert a character to an image, and then to a JSON object containing the glyph matrix.
Finally, jQuery.getJSON loads it, and the callback function simply creates the table.

Demos:

Download:

Ideas to improve it:

  • antialiased characters
  • imageToTable
  • animated charToTable

Any feedback welcome!

News

Derniers articles

Derniers commentaires