Archive | June, 2015

What is clearfix?

18 Jun

This was long overdue,I always quiz about clearfix and is something that I feel every Frontend Developer should know about. But surprisingly almost 50% of candidate could never answer this thing and hence this post

A clearfix is a way for an element to automatically clear its child elements, so that you don’t need to add additional markup. It’s generally used in float layouts where elements are floated to be stacked horizontally.

The clearfix is a way to combat the zero-height container problem for floated elements

A clearfix is performed as follows:

.clearfix:after {
   content: " "; /* Older browser do not support empty content */
   visibility: hidden;
   display: block;
   height: 0;
   clear: both;
}

Or, if you don’t require IE

.clearfix:after {
  content: "";
  display: table;
  clear: both;
}

Normally you would need to do something as follows:


    <div style="float: left;">Sidebar

    <div style="clear: both;">
 

With clearfix, you only need to

<div class="clearfix">
    <div style="float: left;" class="clearfix">Sidebar

    

I hope this helps in understanding the magic behind clearfix.

Creating and running a simple Node.js server

12 Jun
Building a server in JavaScript?

If you’re using lots of JavaScript for the client-side of your app, it may make sense for you to also use JavaScript for the server. Such is the convenience of Node.js.

In this article we’ll go over all the setup and code needed to get a Node.js server loading an index.html file from localhost into your web browser.

What you’ll need

Install Homebrew if you don’t already have it.

Run brew install node in your Terminal. This will install both node andnpm.

Now, run npm install express ––save to install Express.

A simple server.js file

Here’s is all the code you’ll need to serve your index.html page when you visit localhost:8000 in your web browser. Save this server.js file in the same directory as your index.html file.

/* *****************
 * Require Imports *
 * *****************/

var express = require('express');
var path = require('path');

/* ***********************
 * Initialize Middleware *
 * **********************/

// Instantiate the express object
var app = express();

// Use the static assets from the same directory as this server.js file
app.use(express.static(path.resolve("./")));

/* **************
 * GET Requests *
 * **************/

// index.html
app.get('/', function(req, res) {
  res.sendFile('index.html');
});

/* ******************
 * Start the server *
 * ******************/

var port = process.env.PORT || 8000;

var server = app.listen(port, function() {
  console.log('Listening on port:', port);
});

Running the Node.js server
Now, execute the command node server.js in your Terminal to run the JavaScript file that will serve up our index.html page.

Finally, open your browser and navigate to localhost:8000.

And you are done…..

Javascript Gurus to follow

10 Jun

As JavaScript developers, we have quite crazy requirements. The playing field is in a state of constant flux and one of the best ways to keep up is interacting with other developers and reading their code. Blogs, such as the one you’re reading are a perfect amalgamation of these two activities.

Today, I’d like to bring your attention to a number of blogs written by pretty well versed developers, focusing on JavaScript development, that you owe yourselves to bookmark.

  • Find his musings on JavaScript at his blog.
  • Inventor of the JavaScript language.
  • Remember to check out his podcast, as well.
  • Tweets at @BrendanEich
  • Find his musings on JavaScript at his blog.
  • Creator of the JS1K competition
  • Tweets at @kuvos
  • Find his musings on JavaScript at his blog.
  • Contributor to the jQuery and Modernizr projects.
  • Creator of so many jQuery plugins that we’re ethically obligated to use the word buttload.
  • Tweets at @cowboy