WebDAV file upload bookmarklet

I recently had a discussion with a co-worker as to whether or not it would be possible to upload files to a WebDAV server via AJAX and in doing so discovered it is indeed totally possibly to do so and so I went on a little bit of a mission and made a pretty basic interface for apache that replaced the standard directory listing and have also made a bookmarklet.

There’s no demo I’m afraid, however the bookmarklet should work on just about any WebDAV server (at least, it worked on mine…), but you won’t have pretty code highlighting :(.

I’ve set up a github repository but you can use the bookmarklet directly.

Note: This will almost certainly not work in IE and I’ve only tested Firefox and Chrome (latest versions).

EDIT: minor update and moved to GitHub pages instead of using raw which caused problems with the CSS on Firefox.

Pop quiz!

An interesting item from stackoverflow via /r/javascript!

Why does parseInt(1/0, 18) == NaN, but parseInt(1/0, 19) == 18 and parseInt(1/0, 25) == 185011843?

Javascript apparently has the division by zero problem solved, due to (x > 0)/0 being equal to Infinity, an object (constant?) in JS in the same vein as NaN. It interacts with NaN as well, in that Infinity - Infinity or Infinity / Infinity == NaN, but Infinity + Infinity or Infinity * Infinity == Infinity. Also there’s -Infinity, just for fun.

When parseInt()ing the value infinity, internally it must run .toString() to get the value which returns "Infinity". Base 19 has the numbers 0-9 and then goes on to the letters a-i (there’s a table in the stackoverflow article) of which the first letter of "Infinity" matches, returning 18, when you up the base to 25, all the letters up to n are parsed turning the ‘number’ "Infinity" into 185011843. Interestingly that also means that parseInt(Infinity/Infinity, 25) == 14648!

Mousetrap – excellent keyboard shortcuts

Just discovered a great little library called mousetrap.

It enables you to have complex, chained if required, keyboard events bound in a very simple, powerful way with no reliance on an underlying library.

Example usage:

Mousetrap.bind('d', function() {
    Post.next();
});

Mousetrap.bind('a', function() {
    Post.previous();
});

Mousetrap.bind('x', function() {
    Post.delete_selected();
});

Mousetrap.bind(['command+a', 'ctrl+a'], function() {
    Post.selectAll();
});

Find out more, test and download it here, or access the git repository here.

Better timeouts and intervals with Javascript

window.setTimeout and window.setInterval don’t ever seem to have been given any love for a long time. (At least from my limited end-user point of view). I’ve toyed in the past with a better queuing system for them and having the ability to run on clear (especially for intervals), but I’ve actually knocked up a basic script that allows object-oriented timeout events. It still uses the native implementations outside, and I’m sure that this has probably already been done 100 times, but I still wanted to have a go.

This script extends the return from window.setTimeout and window.setInterval to be an object itself that you can stop() and restart(), preserving the function for further use.

var t = window.setTimeout(function(timeout) {
    // timeout argument is the timeout object itself
    console.log('test');
}, 300);
// test
t.restart();
// test

var s = window.setInterval(function(interval) {
    // interval argument is the interval object itself
    Messages.check();
}, 1000);
// Interval
s.stop();
// Interval
s.complete;
// 9
s.cancelled;
// 1

Please note: I’ve only really tested this in Chrome! I can’t see why it wouldn’t work in other browsers, I guess maybe some of these would be protected, but I’m not sure… If that’s the case, it wouldn’t be too difficult to utilise the alternative new Timeout(function(){ }, 400); syntax…

There’s a bit more (not much!) of an explanation here and the code is available here. There are no prerequisites for this code, should run on plain old Javascript.

Edit: Minor update to the script to include the object in the callback.

jQuery Parallax: In action

A lot of the traffic I get to this blog is related to the jQuery Parallax script I wrote quite some time ago. Looking at the code now, I don’t think I’d change too much about it, perhaps use .on('mousemouse', ...) instead of .mousemove(), but mostly it seems to serve it’s function quite well. Since the only real functions being called are .mousemove() and .css() it should still be working with the latest versions of jQuery and unless they decide to majorly change the API, it should continue to work too!

Since my example was on a fairly empty page, I thought I’d link to a couple of sites that are using it in the real world:

If you’re using the script and think your sites is a good example of utilising the parallax effect, please leave me a message!

Bookmarklet: Reload CSS files without reloading the page

Working on styling the contents of popup windows can be frustrating and laborious. But not if you can just reload the CSS…

This bookmarklet will reload all the associated stylesheets in the current document. It looks for all <link/> elements with rel="stylesheet", removes from the DOM and re-adds them appending a ?_=1234567890 (or &_ depending on the URL, where the numbers represent the JS timestamp) into the <head/>.

Hopefully this will help someone suffering similar pain to myself!

Reload CSS (Add the link to the left to your bookmarks to use!)

Please note: I haven’t tested this in IE, but surely it’ll work… Right?

Continue reading “Bookmarklet: Reload CSS files without reloading the page”

Prototype 1.6: Event.live

I’ve recently been using Prototype 1.6 and had a need for a jQuery.live() clone. The following code appears to emulate mouse events well (form submits [and maybe more…] do not work in IE):

Event.live = function(s, e, f) {
    Event.observe(document, e, function(event) {
        if (Element.match(event.target, s)) {
            if (!(f.call(event.target, event))) {
                event.stop();
            }
        }
    });
}

To use this run something like the following:

Event.live('div#doesnt_exist_yet a.button', 'click', function() {
    // run this when the button is clicked
});

Hope this helps!

jQuery Ajaxify Lite

I’ve been using the ajaxify plugin for a while now and felt it needed an update.

Since .live() is supported for more events now, I thought it was time to get it working how I always wanted it to work.

Using the same syntax on links as before, you can specify the target div, using the target=”” attribute and the url is automatically extracted from the href=”” or action=”” attribute meaning you can keep your code simple and valid but have easily Ajax populated content, as since I’m using .live() any future links/forms that match the original selector will continue to be ‘ajaxified’.

There’s a demo here and source code too, as ever.

Also available through Google Code.

simpleGallery 0.2 – Minor updates

I’ve updated the gallery script I created in July. I’ve re-factored the code into a static class for easier modification and have created a config.ini settings file, to save modifying the script itself.

New features:

* Image titles
* Separate configuration
* Standard mode with safe URLs as well as mod_rewrite enabled ‘nice’ URLs
* Added ‘..’ parent folder to galleries

The demo page has been updated and the script is available to download here.