background preloader

JavaScript

Facebook Twitter

Introduction to JavaScript Source Maps. Have you ever found yourself wishing you could keep your client-side code readable and more importantly debuggable even after you've combined and minified it, without impacting performance? Well now you can through the magic of source maps. Basically it's a way to map a combined/minified file back to an unbuilt state.

When you build for production, along with minifying and combining your JavaScript files, you generate a source map which holds information about your original files. When you query a certain line and column number in your generated JavaScript you can do a lookup in the source map which returns the original location. Developer tools (currently WebKit nightly builds, Google Chrome, or Firefox 23+) can parse the source map automatically and make it appear as though you're running unminified and uncombined files. Demo: Get original location The above demo allows you to right click anywhere in the textarea containing the generated source. Real world So... Potential XSSI issues. JSCapture. Contour Overview. Kreate.js | A Tiny jQuery Element Creator. Welcome to the tutorial portion of Kreate! Below are a some snippets and guides to help you become a master of kreation.

Are you ready? Of course you are. You were born ready. You were born awesome. 1. The Basics To Kreate something, just enter in the code below: $.kreate(); Congrats! 2. There are two ways you can kreate elements - express and advanced. To express kreate something, enter a string into the kreate function. Format: $.kreate( ' selector#id.class ' ); selector - Type of element (e.g. div, span, image, ul. . . ) * Just a heads up, you can only add one class to the element. Now that you know the express formatting, let's start expressing ourselves! Express Creating a Single Element Let's try express kreating a div with a class.. $.kreate('div.fancy'); Awesome! Next, let's try something a little fancier. . $.kreate('div.

Great work - You just made this: Express Creating Multiple Elements Yes, oh yes. All you have to do is add a number after your string. . $.kreate('div.fancy', 7); 3. Phew! JS NICE: Statistical renaming, Type inference and Deobfuscation. Visual Event 2. Events in Javascript are often seen as a bit of an enigma. This is odd given that Javascript is very much an event driven language, but it is typically down to their complex nature and difficulty to debug. To this end I've created Visual Event to help track events which are subscribed to DOM nodes.

Introduction Visual Event is an open source Javascript bookmarklet which provides debugging information about events that have been attached to DOM elements. Which elements have events attached to them The type of events attached to an element The code that will be run with the event is triggered The source file and line number for where the attached function was defined (Webkit browsers and Opera only) In addition to being useful for debugging your own code, Visual Event can be used as an educational tool, showing how many web-sites have been authored.

Visual Event is open source software (GPLv2) and a Git repo is hosted on GitHub for you to fork and modify as you wish! Install How it works. A JavaScript survival guide. Are you a programmer who is considering learning JavaScript, but unsure whether it is worth the pain? Then this blog post is for you: I argue that it is worth it and give tips for surviving the language. Why learn JavaScript? The present The main reason for choosing JavaScript is the breadth of its ecosystem: the web platform, Node.js, JSON, NoSQL databases, Cordova/PhoneGap, automating PhotoShop and many other apps, etc. Compared to Java, I like the interactivity of JavaScript and the web platform. It is easy to quickly try out things and to inspect what is currently going on. I also like the flexibility of the language. The future ECMAScript 6 [1], the upcoming version of JavaScript fixes many of JavaScript’s problems: not enough data structures, limited built-in support for inheritance, no built-in support for modules, shadowing this, functions playing too many roles, … Additionally, there is much innovation and experimentation around: Take your time to get to know JavaScript More safety.

Bolshchikov/js-must-watch. jQAPI - Alternative jQuery Documentation Browser. Promise Pipelines in JavaScript - sitr.us. Promises, also know as deferreds or futures, are a wonderful abstraction for manipulating asynchronous actions. Dojo has had Deferreds for some time. jQuery introduced its own Deferreds in version 1.5 based on the CommonJS Promises/A specification. I’m going to show you some recipes for working with jQuery Deferreds. Use these techniques to turn callback-based spaghetti code into elegant declarative code. The basics of jQuery Deferreds A Deferred is an object that represents some future outcome. In jQuery’s terms a promise is a read-only view of a deferred. Here is a simple example of creating and then resolving a promise: function fooPromise() { var deferred = $.Deferred(); setTimeout(function() { deferred.resolve("foo"); }, 1000); return deferred.promise();} Callbacks can be added to a deferred or a promise using the .then() method.

FooPromise().then( function(value) { // prints "foo" after 1 second console.log(value); }, function() { console.log("something went wrong"); }); Conclusion. Modeln.github.io/sDashboard/example/ex.html. Moment.js | Documentation. Moment(String, String); moment(String, String, String); moment(String, String, Boolean); moment(String, String, String, Boolean); If you know the format of an input string, you can use that to parse a moment. moment("12-25-1995", "MM-DD-YYYY"); The parser ignores non-alphanumeric characters, so both of the following will return the same thing. moment("12-25-1995", "MM-DD-YYYY"); moment("12\25\1995", "MM-DD-YYYY"); The parsing tokens are similar to the formatting tokens used in moment#format.

Year, month, and day tokens Week year, week, and weekday tokens For these, the lowercase tokens use the locale aware week start days, and the uppercase tokens use the ISO week date start days. Hour, minute, second, millisecond, and offset tokens Locale aware date and time formats are also available using LT LTS L LL LLL LLLL. Z ZZ were added in version 1.2.0. S SS SSS were added in version 1.6.0. X was added in version 2.0.0. moment('2012 juillet', 'YYYY MMM', 'fr'); moment('2012 July', 'YYYY MMM', 'en'); Dbushell/Pikaday. Nnnick/Chart.js. Jstayton/jquery-marcopolo. Methods | jCanvas Docs. The <canvas> element is a new element apart of HTML5. It allows you to draw shapes, paths, images, and other drawings on a blank element called the canvas.

Creating a canvas Before you can draw on the canvas, you need to create one. Of course, the canvas can be any width/height you want. You may also wish to give the canvas an ID (for future reference). As a side note, you cannot accurately set a canvas's width and height via CSS; you can only do so through the canvas element's width and height attributes. Importing jCanvas You also need to add jCanvas to your page somehow (usually a <script> element will do). Drawing. Modernizr: the feature detection library for HTML5/CSS3. Function.apply and Function.call in JavaScript. In order to explain how the createDelegate function works in the last post, we have to understand JavaScript closures and Function.apply(). The apply() method is the easiest subject to tackle, so we'll start there and work up. Every function in JavaScript has a number of attached methods, including toString(), call(), and apply().

If it sounds odd to you that a function might have its own methods - then remember that every function in JavaScript is an object. Read this article for a refresher. You might also wonder what the difference is between a function and a method. I believe the descriptors 'function' and 'method' are just a JavaScript convention. Because functions are objects they can have their own properties and methods, and we can treat them like data. Let's start with the following code: Here we have a global function by the name of f(). f() uses the this keyword to reference x, but notice we don't invoke the function through an instance of an object. How to measure the speed of JavaScript execution. If you're curious about the time it takes for your page to load and were wondering if there was something that could tell you what's going on besides human observation, then this excerpt from Even Faster Web Sites will get you pointed in the right direction.

The easiest, most straightforward, and probably least precise way to measure latency is via human observation; simply use the application on your target platforms and ensure that performance is adequate. Since ensuring adequate human interface performance is only about pleasing humans, this is actually a fine way to perform such measurements (obviously, few humans will be able to quantify delays reliably in terms of precise whole or fractional second measurements; falling back to coarser categorizations such as “snappy,” “sluggish,” “adequate,” and so on does the job). Manual code instrumentation is really straightforward. Let’s say you have an event handler registered on your page, as in: <div onclick="myJavascriptFunction()"> ...

NodeJS

ColorBox, A jQuery Lightbox. A lightweight customizable lightbox plugin for jQuery Fork me on GitHub View Demos Download Released under the MIT License. Supports photos, grouping, slideshow, ajax, inline, and iframed content.Lightweight: 10KB of JavaScript (less than 5KBs gzipped).Appearance is controlled through CSS so it can be restyled.Can be extended with callbacks & event-hooks without altering the source files.Completely unobtrusive, options are set in the JS and require no changes to existing HTML.Preloads upcoming images in a photo group.Currently used on more than 1.9 million websites. Instructions & Help The FAQ has instructions on asking for help, solutions to common problems, and how-to examples.

Usage Colorbox accepts settings from an object of key/value pairs, and can be assigned to any HTML element. Settings Public Methods Event Hooks // Example of using an event listener and public method to build a primitive slideshow:$(document).bind('cbox_complete', function(){ setTimeout($.colorbox.next, 1500);}); Hey,

Minifier

Sizing and Positioning Fancybox | GiveGoodWeb. You’ve probably seen Fancybox all over the place. In a way, it’s the living-successor of ThickBox and Lightbox. I was recently on a project where I had to both size and place it. Sizing it is easy. Say you have a link tag with an id of opener, like this: Google To create and size the fancybox that will open when it’s clicked, you’d just add the following javascript to your page: Note that pixels are assumed, you should not say something like “390 pixels” like you would in css, it won’t work then. So, above we have defined a fancybox that is 390 pixels wide, and 400 pixels high.

(Note this is based on Fancybox 1.3 – I can’t say if it’ll work with other versions.) Fancybox puts everything in a layer with an id of “fancybox-wrap” – so we can easily manipulate it with basic css, like so: You can do the same with padding, maybe even positioning. Hope that helps someone! Advanced Debugging With JavaScript. When used effectively, JavaScript debuggers help find and squash errors in your JavaScript code. To become an advanced JavaScript debugger, you’ll need to know about the debuggers available to you, the typical JavaScript debugging workflow, and code requirements for effective debugging. In this article, we’ll discuss advanced debugging techniques for diagnosing and treating bugs using a sample web application. On accessibility#section1 This article highlights strengths and differences between debugging tools, and shows how we perform advanced JavaScript debugging tasks.

The debuggers#section2 With an increasing selection of good debuggers available, JavaScript programmers can gain a lot from learning how to use them. Currently, there are debugging tools available for all major web browsers. Currently, Firebug and Dragonfly are the most stable options. Familiarize yourself with multiple debugging tools—you never know in which browser the next bug will arise. Debugging workflow#section3. jStorage - simple JavaScript plugin to store data locally. jQuery Events: Stop (Mis)Using Return False. Probably one of the first topics covered when you get started learning about jQuery events is the concept of canceling the browser’s default behavior. For instance, a beginner click tutorial may include this: This function toggles the hiding and displaying of #mydiv, then cancels the browser’s default behavior of visiting the href of the anchor tag. It is in these very first examples that bad habits are formed as users continue to use return false; whenever they want to cancel the default browser action.

I am going to cover two very important topics in this article relating to the canceling of browser events: Use the right method for the job: return false vs. preventDefault, stopPropagation, and stopImmediatePropagationTop, bottom or somewhere in the middle: where in the event callback should you cancel default behavior?

Use the Right Method for the Job The main reason return false is so widely misused is because it appears to be doing what we want. What return false is really doing 1. 2.

GoogleMap

Performance:Leak Tools. Strategy for finding leaks When trying to make a particular testcase not leak, I recommend focusing first on the largest object graphs (since these entrain many smaller objects), then on smaller reference-counted object graphs, and then on any remaining individual objects or small object graphs that don't entrain other objects. Because (1) large graphs of leaked objects tend to include some objects pointed to by global variables that confuse GC-based leak detectors, which can make leaks look smaller (as in bug 99180) or hide them completely and (2) large graphs of leaked objects tend to hide smaller ones, it's much better to go after the large graphs of leaks first.

A good general pattern for finding and fixing leaks is to start with a task that you want not to leak (for example, reading email). For example, start with bringing up the mail window and closing the window without doing anything. What leak tools do we have? Leak tools for large object graphs leak-gauge leak-monitor Leaksoup Purify. Head JS. Extreme JavaScript optimization. 2010 update: Lo, the Web Performance Advent Calendar hath moved Dec 20 This article is part of the 2009 performance advent calendar experiment. Today's article is a second contribution from Ara Pehlivanian (here's the first).

There's a Belorussian translation provided by Patricia. Thanks! Ara Pehlivanian has been working on the Web since 1997. There's an odd phenomenon underway in the JavaScript world today. The thing of it is, it's pretty easy to put your foot to the floor and get up to 60 mph. Variables Let's take a look at one of the staples of programming, the variable.Some languages require you to declare your variables before using them, JavaScript doesn't.

Function doSomething(val) { count += val; }; Does count have a value assigned to it outside the scope of doSomething? Loops Searching the scope chain for where count is declared in the example above isn't such a big deal if it happens once. For (var i = 0; i < arr.length; i++) { } Did you see it? For (var i = 100; i--; ) { } Caching. JavaScript and HTML DOM Reference. Objektorientierte Programmierung in Javascript - Browsergame-Entwicklung - GalaxyNews.de.

JSON