Handy! RGB to HSL and RGB to HSV color model conversion algorithms in JavaScript - Axon Flux // A Ruby on Rails Blog

Here is a set of additive color model conversion algorithms that I found published on Wikipedia and have implemented in JavaScript.

Beware: Coffeescript "in" is not the Javascript "in"

The Javascript in operator does what Hash#has_key? does in Ruby: Return whether an object has a property.
However, Coffeescript has its own in operator that checks for array inclusion. To check whether an object has a property, use of:

Javascript

'name' in {name: 'Horst'} # => true

Coffeescript

# wrong
'name' in {name: 'Horst'} # => false

# correct
'name' of {name: 'Horst'} # => true
1 in [1,2,3] # => true

True story.

JavaScript “Stale Practices” | benmccormick.org

The linked article lists a number of techniques that were best practices with ES5, but have better alternatives in modern JavaScript.

Best practices don’t last forever. This is especially true when a field is changing fast, and JavaScript development has changed a lot over the past 10 years. The old best practices go stale, and new ones take their place. Here are 5 JavaScript best practices that have gone stale recently.

Javascript: Wait until an image has finished loading

The attached ImageLoader helper will start fetching an image and return an image that is resolved once the image is loaded:

ImageLoader.load('/my/image.png').then(function(image) {
  ...
});

The image argument that is yielded to the promise callback is an HTMLImageElement. This is the kind of object you get when you call new Image().

Don't use "self" as a Javascript variable

You might sometimes use self to capture the context of this before it is destroyed by some function.

Unfortunately self is also an alias for window, the global top-level object. Save your future self some headaches and use another name like me instead (Coffeescript chose to use _this).

Chart.js - a promising JavaScript charting library with MIT-license

Chart.js seems to be a good alternative to Google's Chart API and other commercial chart drawing libraries.

  • good looking charts
  • canvas based (means less memory consumptive, but no interactivity out of the box)
  • highly configurable
  • good API and documentation
  • just 4.5 kilobytes
  • MIT license
  • Browser support: all browsers supporting the canvas element (for IE8 and below, use the polyfill as describes in the [chart.js documentation...

Howto remove the location hash without causing the page to scroll

Set the hash to a dummy hash which doesn't hit any id at your page, for example:

window.location.hash = "_";

Note

  • If you'd set the hash to "" it causes the page to scroll to the top because the hash "#" by itself is equivalent to "_top".
  • If you'd set window.location.href = "..." to get rid of the "#", you cause the browser to reload the page what is most likely not intended.

Javascript: Read params from url

There is no build in functionally in jQuery and Prototype to extract params from a url.

  1. You can use this library (not tested): jquery-deparam

  2. Use Unpoly and the following code snippet

Solutoire.com › Flotr Javascript Plotting Library

Flotr is a javascript plotting library based on the Prototype Javascript Framework (version 1.6.0.2 at the moment) and inspired by Flot (written by Ole Laursen).

Should You Use JavaScript Library CDNs? « Lickity Split

sing Google’s JavaScript Library CDN comes with a 1/3 of a second tax on missing. (Note that a tax like this applies to opening connections to a any new host: JavaScript Library CDNs, advertisers, analytics and visitor tracking, etc. This is why you should try to reduce the number of different hostnames you serve content from.)

What's wrong with defining JavaScript variables within if blocks? - Stack Overflow

Because javascript has something called "Hoisting" which makes your code do things it doesn't look like it should be doing. Basically, that means a javascript interpreter will move all var declarations, regardless of where they are in the body of a function, to the top of the function body.

Cross-Origin Resource Sharing - Wikipedia

Cross-Origin Resource Sharing (CORS) is a browser technology specification, which defines ways for a web service to provide interfaces for sandboxed scripts coming from a different domain under same origin policy. CORS is a modern alternative to the JSONP pattern. While JSONP supports only the GET request method, CORS also supports other types of HTTP requests. Using CORS enables a web programmer to use regular XMLHttpRequest which supports better error handling than JSONP. On the other hand, JSONP works on legacy browsers that do not have C...

Open the on-screen keyboard on an iPhone or iPad with Javascript

There is no way to do it. This behavior is by design. You lose.

Writing Fast, Memory-Efficient JavaScript

JavaScript engines such as Google’s V8 (Chrome, Node) are specifically designed for the fast execution of large JavaScript applications. As you develop, if you care about memory usage and performance, you should be aware of some of what’s going on in your user’s browser’s JavaScript engine behind the scenes.

Hoptoad and Javascript, Sitting in a Tree, S-E-N-D-I-N-G - GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS

We didn’t want to leave front-end developers in the dark when their Javascript throws errors, so we’ve added a Hoptoad notifier for Javascript!

Delay your Jasmine tests until the document is ready

To delay your entire Jasmine test suite until the DOM is ready, add the following:

beforeAll(function(done) {
  $(done);
});

BubbletreeJS: A Javascript data visualization library

BubbleTree is a library for interactive visualization of hierarchical data. Originally developed mainly for spending data, the library is now completely independent from the OpenSpending platform. BubbleTree is built on top of jQuery and RaphaelJS.

See the Demo or the tutorial.

JavaScript Coordinates

To move elements around we should be familiar with coordinates. Most JavaScript methods deal with one of two coordinate systems:

  • Relative to the window(or another viewport) top/left.
  • Relative to the document top/left.
    It’s important to understand the difference and which type is where.

RaphaelJS: A Javascript vector graphics library

Raphaël is a small JavaScript library that should simplify your work with vector graphics on the web. If you want to create your own specific chart or image crop and rotate widget, for example, you can achieve it simply and easily with this library.

Moment.js - A lightweight javascript date library

A lightweight javascript date library for parsing, manipulating, and formatting dates.

Apprise - The attractive alert alternative for jQuery

An alert alternative for jQuery that looks good. Apprise is a very simple, fast, attractive, and unobtrusive way to communicate with your users. Also, this gives you complete control over style, content, position, and functionality. Apprise is, more or less, for the developer who wants an attractive alert or dialog box without having to download a massive UI framework.

How to get the currently selected text in Javascript

Simple:

window.getSelection().toString();

Browser support: IE9+, Android 4.3+, Safari 5+