jQuery Twitter plugin update

A lot of users requested a demo for my jQuery Twitter plugin. It has been a while since I updated the plugin, so I downloaded the latest version and while looking thought the code found couple of bugs. So, here comes updated and fixed jQuery Tweeter plugin - jTwitter 1.1.1.

In this release, I fixed a little bug that would not allow you request Tweets without number of posts like this:

// Defaults to 5 latest posts
$.jTwitter('jQueryHowto', function(posts){
  //Callback function with the user data
});

You can specify the number of posts you want to be fetched like so:

// Get latest 13 posts
$.jTwitter('jQueryHowto', 13, function(posts){
  //Callback function with the user data
});

Also, I created a simple demo page for the plugin here. Please visit and see the code and also note that with this plugin you can create a custom Twitter badge with no design limitations!

Update: Pushed the project to GitHub.

jQuery code / syntax guidelines

We all write our own jQuery code and since creating custom jQuery plugins is so easy, we all create our own jQuery plugins. And all of us have our own code syntax preferences. For example:

function myFunction() {
    ...
}

// some prefere it like this
function myFunction()
{
    ...
}

If you want to publish your jQuery plugins following jQuery core code writing guidelines is a good idea. This would make your code more easier to read to other jQuery developers. Follow the link above and go through the guidelines (it will only take a few minutes). The guidelines page shows gives you general guidelines, but I went through the jQuery core code and found several additional ones. I lay they out at the end of this post.

In short, the official guidelines are as follows: (Taken from official docs)

  1. Use tabs to indent your code
  2. Use liberal spacing":
    function myFunction ( myVar, myVar2 ) {
        // Pay attention to spaces around
        // the brackets and curly brackets
    }
  3. Use strict equality checks whenever possible (===)
  4. Braces should always be used on blocks and statements should not be on the same line as a conditionals:
    // NO:
    if ( true ) blah();
    
    // YES:
    if ( true ) {
       blah();
    }
  5. When checking an object type:
    String: typeof object === "string"
    Number: typeof object === "number"
    Function: jQuery.isFunction(object)
    Array: jQuery.isArray(object)
    Element: object.nodeType
    
    See full list on official docs page (link above)
  6. Don't use "string".match() for RegExp, instead use .test() or .exec()
  7. Node related rules:
    • .nodeName should always be used in favor of .tagName.
    • .nodeType should be used to determine the classification of a node (not .nodeName).
    • Only attach data using .data().

Now let’s see other additional rules that I could spot reading the jQuery core code.

Additional jQuery code / syntax guidelines:

  1. The first thing that I noticed was multiline comments did not use common /* comment */ syntax. In jQuery core all multiline comments use line comment syntax // comment.
    // This is an example of multiline
    // comment syntax used in jQuery core.
  2. Local variables are declared and initialized on one line just below the function declaration with no extra line:
    function someFunction () {
        var target = arguments[0] || {}, i = 1, name;
    
        // Empty line and then the rest of the code
    }
  3. When declaring objects, there is no space between property name and colon:
    {
        myName: "",
        myFunc: function( ) {}
    }
  4. All strings are in double quotes " ", not single quotes ' ':
    var myMarkup = "<a href='/a'>a</a>";
  5. The last but not least, variable naming uses camelCase.

Dynamically create iframe with jQuery

This article explains and shows how to dynamically create an iframe in your HTML document’s DOM using jQuery and/or JavaScript. It also gives you an example code. This post is not extensive explanation of how to manage and work with iframes using jQuery. This post simply shows you how to dynamically create iframes using jQuery and JavaScript and serves as a note.

Creating iframe is similar to creating any DOM element. All you need to do is to use jQuery factory like this:

// Create an iframe element
$(‘<iframe />’);

// You can also create it with attributes set
$('<iframe id="myFrame" name="myFrame">');

// Finnaly attach it into the DOM
$('<iframe id="myFrame" name="myFrame">').appendTo('body');

Don’t forget that the iframe source is just an iframe’s attribute. So you can set that just like any other attribute.

// Setting iframe's source
$('<iframe />').attr('src', 'http://www.google.com'); 

UPDATE:

As BinaryKitten point’s out below, with jQuery 1.4 you can do it using the following syntax:

// Set attributes as a second parameter
$('<iframe />', {
    name: 'myFrame',
    id:   'myFrame',
    ...
}).appendTo('body');