Posts

Showing posts with the label javascript

Running node.js on windows without using Cygwin

Finally node.js is available as a windows binary without requiring Cygwin to be installed. As of writing this post you can download the node.js binary 0.5.6 . Currently npm does not work with node.js on windows. You need to download and use ryppi.py instead of npm . It works similar to npm but requires python. To setup node.js first set the PATH and NODE_PATH to the where you have copied node.exe.              set PATH=%PATH%;c:\node              set NODE_PATH=c:\node It is better to set these environment variables in the System Properties so that you don't have to set these variables every time. Follow the following steps to do that. Right-click  Computer , and then click  Properties . Click the Advanced system settings in the left side bar. Click the  Advanced  tab. Click  Environment variables . Append the node path to the PATH variable Create a new variable...

TIL: In JavaScript function.length returns the number of arguments it supports

function test(a, b, c) { } alert(test.length); In this piece of code if you use .length on a function it returns the number of named arguments that this function accepts.

Conditional compilation in IE

I normally know about conditional compilation of JS in IE using <!--[if gt IE 6] --> but here is something new I figured out today. var IE; //@cc_on IE = parseFloat(navigator.appVersion); The best part here is that the JS code stays in your JS files and does not uglify the HTML if you ever have to use conditional compilation. Here is some more information about it from MSDN article Conditional Compilation (Windows Scripting - JScript) .

Speed up your JavaScript

Image
This post is just for my own reference. Here are the slides and the video of the presentation by Nicholas Zakas. Goes into great detail in ways to improve the speed of your javascript code. Speed Up Your JavaScript View more presentations from Nicholas Zakas The article summarizes most of the points in the slides and video http://james.padolsey.com/javascript/zakas-javascript-performance-tips/

COMET for Chirp User Streams - II

After almost giving up I finally figured out a way to use the Chirp User Streams . Not the best way to use it but I guess it just might work. Create a XHR request and abort it ever other minute. It will complete the request and make a new request. Try and keep the connection alive. This is something that facebook does in its chat. Thought there the connection is aborted by the server. In this case make sure that you abort and make the connection the very next second. It should be possible to use the same connection to reconnect to the server. But I have not tested it extensively to see how it will work in a real world situation. For now it works and hopefully when I again get to work on it I will write something more about how I use the ChirpUserStreams.

COMET for Chirp User Streams

Image
First there was AJAX and to give it company there was COMET . Incidentally both names of popular disinfectants in North America. In JavaScript AJAX allows the client to PULL information from the server and COMET is the technology where the server PUSHES down information to the client. I got interested in COMET after going through Chirp User Streams . Thinking it would be a trivial task to call a XHR and keep listening to it. In my mind I kept thinking that it would be stuck on readyState 3 and it will never get to 4. Since it would be stuck at 3 I could use a timer and after every few seconds keep checking for the change in length of xhr.responseText.length . Technically it should work like that.. BUT.. the way IE has implemented XHR it is not possible to check the responseText till it reaches 4. Which means that the request has timed out or ended. That somehow makes no sense at all. I figured out it was time to investigate and see how Facebook and GTalk were able to work on IE if i...

Adobe Stratus - P2P networking using your webbrowser

With the launch of Flash 10 there is a P2P technology Stratus built right into flash. When it was launched I was not really interested in it as it takes time to deploy the latest flash runtime. But in less than an year Flash 10 has been deployed on more than 90% of the systems worldwide (According to these stats on adobe.com ). I personally don't like flash but flash is not just about animations and videos. Its a lot more than that. And these things are what will keep flash alive even after the onslaught of "HTML5". The problem is HTML5 is made out to be this magical solution that will rid the world of Flash. Unfortunately with people like Steve Jobs promoting HTML5 as the solution to all Flash problems most people blindly start to believe it. The reason Flash really works is that the deployment is so large and most people have it installed even if they don't know about it. iPhone got away without using flash as they had partnered with Google when they first launched...

Finally finished off with vivekjishtu.com

I have been planning to revamp the homepage for quite a while now. Finally I have been able to do something about it. I finished off the non-javascript part a week back. I was hoping to do a lot more with the JavaScript part but shelved any major plans for now. In the process I ended up with a leaner page, with less than 7KB of JavaScript. I was planning to use YUI but in the end got rid of it. The JavaScript part is an add-on and even if you turn off javascript you should be able to see the complete content. I wanted to use JSON and get data from all possible places and show it on the homepage. The main problem with this approach is that the website becomes dependent on multiple sources. Incase flickr, blogger or twitter is down the page will take a long time to load. To get rid of the problem all the scripts are loaded after the page has loaded. Even if any of the scripts fail the page does not end up looking broken.

Wacking my brain with localStorage and globalStorage in Javascript

Most of the browsers these days allow you to store a lot of data instead of using cookies. I am still trying to make a library which will allow you to store data on the browser no matter if you have localStorage, globalStorage, flash or the good old cookies. The thing about using flash is that you need to wait for it to initilise and that is a little bit of a problem. I hope to release the library under the BSD license. Not sure if anyone would be interested, but I really need to get it done for fefoo .

A very brief review of the javascript libraries

I've have never really liked any JavaScript library and i normally believe in writing my own code so that I know whats going on and where it is. But for a change with an open mind I began looking at the different JavaScript libraries that are available. I tried out the following. dojo ExtJS YUI JQuery prototype dojo This one is really extensive and tries to achieve anything and everything. It seems easy to grasp and I was able to get going in less than a few minutes. But given the fact that it is really extensive it also adds to the weight. I don't think I would be using the complete library in any of my projects but if used as a learning resource it can be really great. ExtJS This is one library I have seen from the very beginning I have always liked it. The toolkit is really professional looking and seems to fit the bill but the only hitch is the license, its under GPL3 . If you don't mind paying then you can get it with a commercial license. This is one of the best lib...

Rendering HTML elements according to the system - I

I like things which look and feel like my OS. But I have rarely come across a site which looks and feels like an application running on my computer. Not like they are supposed to look like that but I have still been trying to figure out how they would look if the site could automatically change according to the user settings. In my quest to figure this one out, the first and the foremost thing to look at are the fonts. Most browsers have the magical property which allows you to get the font settings on the OS. This line should be in the menu font. This line should be in the caption font. On most browsers you should see the first line rendered with the font used in the menu 's and the second line rendered in the font used in the caption 's. Funny thing caption does not work in FireFox it works in Opera and IE. Getting down to the code its just a one line code. Just set the font property to menu or caption and thats all that you need to do. font: menu; font: caption; That is...

Generating regular expression at runtime in javascript

I have never been good with regular expressions. I normally search for the regular expressions online and use it, I can never seem to get them into my brains. Lately I've been working on things where I've had to generate a regular expression at runtime. It is a trivial thing but I was unable to find it when I look for it. So in case you are looking for generating a regular expression at runtime this code snippet might help you out. var data = "This is a string and I want to replace all the a's"; var re = new RegExp("a", "ig"); alert(data.replace(re, "m")); This post is more like a reminder for me.

A better alternative to window.onload

This code can be used as a better alternative to window.onload . We use main as the function from where the execution begins. We also have a __app_started varible which tracks if main has been called so that it is not called more than once. Basically by this technique main gets called the moment body has been completed. If you use window.onload it waits till all the images and CSS have been loaded and in most case that can really be a while. var __app_started = false; if(document.addEventListener) document.addEventListener("DOMContentLoaded", main, false); else { document.write("<scr" + "ipt id=__ie_onload defer src=javascript:void(0)><\/scri" + "pt>"); var script = document.getElementById("__ie" + "_onlo" + "ad"); script.onreadystatechange = function() { if(this.readyState == "complete") main(); } } window.onload = main; // If all else fails function main() { ...

#include or include a file in javascript at runtime

I had written about require or #include in javascript and it works but sometimes you don't want it to be added it using DOM. This function uses DOM incase the body has been loaded or uses document.write to load the file. function $include(url) { if(document.body) { var script = document.createElement("script"); var head = document.getElementsByTagName('head').item(0); script.src = fileUrl; head.appendChild(script); } else document.write("<script type='text/javascript' src='" + url + "' ></sc" + "ript>"); } If you look carefully the name of the function is $include . It just makes it stand out other than that you could name it whatever you want. This just makes the code really clean and no more messy html files with 10 script tags to load all the files. It just makes life simpler if you have too many script files to load.

SimpleXML for Javascript available for download

If you've used SimpleXML in PHP5 and higher you can use the same functionality in javascript using this library. Its just a single file and you can convert nearly any XML file into javascript object which you can use in your javascript web page or application. The code is hosted on Google Code . You can either check it out using svn or download the zip archive. Download the latest version