Wednesday, March 11, 2009

jQuery.LocalScroll 1.2.7 released

Notice

I've pretty much stopped updating this blog, but the plugin development is still on-going. You can find the link to the Github project page at the bottom of the article.

In this release I cleaned up the code a little and added some features. Also removed a few old (aka deprecated) stuff that were still hanging around.

Enhancements

  • Added some misc enhancements and cleanup the code.
  • Updated the plugin to take advantage of recent scrollTo additions.

Changes

  • The element that triggered the scroll cannot be accessed anymore from within the onBefore, you can bind your own click (or w/e) to them in order to add a class or things like that.
  • settings.persistent is no longer supported (was deprecated).

Features

  • The set of settings can be accessed from within the onBefore as the 'this'.
  • The hash (#foo) is set to the URL before scrolling, so the back button works accurately (when scrolling the window).
  • The option 'hash' doesn't make the window jump when scrolling overflown elements
  • $.localScroll.hash now resets the element scroll to (0,0) by default. You can set the setting 'reset' to false to avoid this.
Enjoy!

Links

Monday, March 9, 2009

jQuery.ScrollTo 1.4.1 released

Fixes

  • The plugin accepts floating numbers.
  • Using jQuery.nodeName where neccessary so that this works on xml+xhtml.
  • The max() internal function wasn't completely accurrate, now it is 98% (except for IE on quirks mode but it's not too noticeable).

Features

  • The target can be 'max' to scroll to the end while keeping it elegant.
  • The plugin works on quirks mode.

Enhancements

  • Default duration is 0 for jquery +1.3. Means sync animations.
  • Rewrote $.fn.scrollable() again. The plugin works on all major browsers (FF, IE, Safari, Opera, Chrome), on all versions, compat & quirks modes, even for iframes.
  • In addition to window/document, if html or body are received, the plugin will choose the right one.

Links

Monday, November 17, 2008

Haskell Functions for Javascript

Introduction

I've been doing a small Haskell project for the Uni and got really interested in this language and the Functional paradigm in general.

I suddenly felt like porting some of this cool functionality to Javascript.

Currying

I started by allowing functions to be curried. Here's a demo showing the script I made.

This is quite a fun way of making functions that derive from other functions.

Haskell functions

I then felt like porting Haskell's functions. Seemed doable considering Javascript has Higher Order Functions and we now can curry them.

I ported many Prelude's methods as well as Char methods.

Special features

Apart from currying, these functions can also work on strings, the same way as they do for lists (arrays). You don't need to worry about splitting(before) or joining(after). I added this because that's how Haskell's functions work.

These functions also work on maps/object/associative arrays. This might not be true for some functions where this was too complicated. This was the best adaptation of Haskell's Tuples that I could think of. A couple of functions like fst and snd don't have a concrete application for now.

In case I didn't make myself clear on this one, all the functions I made can be automatically curried. They're all contained in one namespace, that is, window.hs. I added a method called: hs.global() that will import all the functions to the global namespace. The magic currying function is available as hs.curry.

Operators

Haskell's operators are regular functions. I did imitate this adding most operators as methods. The only problem is that to reference them, you need to use something like hs['+'] instead of hs.+ as one would like.

To defeat this, I made hs a function, it provides a small shortcut for these cases. As an example: hs['+'](1,2) == hs('+',1,2).

I think the second option does look better. These functions can also be curried. hs._ will work the same as hs, but inverting the (2) arguments.

I added the dot operator for function composition, it's quite a powerful function.

 

That's it, the script is licensed under BSD, check the demo and play with it!

Links

Downloads

Monday, November 3, 2008

Fast Trim Function for Javascript

Introduction

Following Steven Levithan's old post about string trimming.

I decided to give it another try after my first proposal.

I made a trim version, based on my aforementioned first attempt. It seems to be much faster on any browser and with different (string) lengths and whitespaces.


First of all...

I want to make clear that this version doesn't trim the exact same characters that other versions do.

In my first version, I simply tried to improve Steven's version without really giving it a deep thought.

This time I thought:Are all those characters really relevant?.

After thinking about this for a while I decided that, whatever /\s/ matches, should be trimmed.

Why ? because most major js libraries use the regex version. If their users are content, then those are the needed characters. Also, if my function would actually do better than others, it could go directly to jQuery's core (there's a ticket for that).

So I created a test page. The results weren't equal for all browsers, IE and Safari 3 yielded far less charCodes. I decided these are the ones I want to trim.


My Trim function

So, the basic modification I made to my previous version, was to check whether the charCode is lower than 33 instead of the whole map/object.

This one takes much less code and is quite faster, I humbly named it "myBestTrim", here it is:

// Licensed under BSD
function myBestTrim( str ){
 var start = -1,
  end = str.length;
 while( str.charCodeAt(--end) < 33 );
 while( str.charCodeAt(++start) < 33 );
 return str.slice( start, end + 1 );
};

The Benchmark

If you want to try the benchmark, get in here. The number should you care about (in my opinion) is the minimum. That's probably the one that ran a lower CPU level.

That test uses a string with 10K characters. Sounds like a lot but this blog's homepage has 55K of html. As oposed to a regex-based trim, this one should scale pretty well, because it doesn't need to check all the string.

That's actually why I removed any regex-based approach from this test, because they'd take too long.

If you have Firebug, I'd advice you to turn it off before getting into this test.

I made a similar test with small strings. It uses a string of 30 characters with 3 whitespaces on each side. In this case, the difference was smaller but still noticeable.

The only situation where another function was faster was on IE, with a small string WITH whitespaces. In this situation, jQuery's trim (the typical regex-based trim) was slightly faster, my trim got 2nd place.

Most situations don't really require much trimming and critical situations have very large strings, so I think this function scales well on those 2.


Conclusion

If all goes well, I'll put this function into jQuery's core. I'd like though, to get some results from other users, to verify my trim is really fast and effective.

Here's the ticket requesting a faster trim for jQuery: #2279.

11/6/08
Removed a dispensable check, thanks Andrea Giammarchi.

Thursday, September 11, 2008

jQuery.ScrollTo 1.4 released!

Fixes

  • Fixed the problem when scrolling the window to absolute positioned elements on Safari.
  • Fixed the problem on Opera 9.5 when scrolling the window. That it always scrolls to 0.

Features

  • Added the settings object as 2nd argument to the onAfter callback.
  • The 3rd argument of scrollTo can be just a function and it's used as the onAfter.
  • Added full support for iframes (even max scroll calculation).
  • Instead of $.scrollTo, $(window).scrollTo() and $(document).scrollTo() can be used(even for iframes).
  • Added $().scrollable() that returns the real element to scroll, f.e: $(window).scrollable() == [body|html], works for iframes too.

I rewrote the scroll limit calculations part, in order to fix the 2 bugs. If you notice any problem, please report.

This version should be perfectly compatible with the latest versions of SerialScroll and LocalScroll.

I tested this only with jQuery 1.2.6 (from trunk) but it should work well even with pretty old versions of jQuery.

Tested my demos and some iframe experiments on IE 6/7, FF 2/3, Safari 3 and Opera 9.2/5. All worked as expected. Enjoy!

Links