Updates from April, 2010 Toggle Comment Threads | Keyboard Shortcuts

  • Mohammad Jangda 1:50 pm on February 25, 2011 Permalink | Reply
    Tags: , , script, timer   

    JavaScript Countdown Timer 

    I was in search of a JavaScript countdown timer (mainly because I’m too lazy to build it out myself — and because I’m sure millions before me have done the same), but Google wasn’t doing me much good. Most scripts would modify the DOM, directly but not make it easy to programmatically intercept the remaining time.

    So, I picked one of the cleanest scripts and cleaned it up a bit.


    /**
     * A sweet js countdown timer with a custom callback that gives you a JSON object!
     * Heavily modified code originally found on http://www.ricocheting.com/code/javascript/html-generator/countdown-timer
     *
     * @param string|Date String representation of when to countdown to. Date objects probably work too
     * @param callback Function triggered when the interval has passed
     * @param int Number of milliseconds for the timeout. Defaults to 1000 (1 second)
     *
     * @return object Returns a JSON object with properties: days, hours, minutes, seconds
     */
    timer = function(endDate, callback, interval) {
        endDate = new Date(endDate);
        interval = interval || 1000;
    
        var currentDate = new Date()
            , millisecondDiff = endDate.getTime() - currentDate.getTime() // get difference in milliseconds
            , timeRemaining = {
                days: 0
                , hours: 0
                , minutes: 0
                , seconds: 0
            }
            ;
    
        if(millisecondDiff > 0) {
            millisecondDiff = Math.floor( millisecondDiff/1000 ); // kill the "milliseconds" so just secs
    
    		timeRemaining.days = Math.floor( millisecondDiff/86400 ); // days
    		millisecondDiff = millisecondDiff % 86400;
    
    		timeRemaining.hours = Math.floor( millisecondDiff/3600 ); // hours
    		millisecondDiff = millisecondDiff % 3600;
    
    		timeRemaining.minutes = Math.floor( millisecondDiff/60 ); // minutes
    		millisecondDiff = millisecondDiff % 60;
    
    		timeRemaining.seconds = Math.floor(millisecondDiff); // seconds
    
            setTimeout(function() {
                timer(endDate, callback);
            }, interval);
        }
    
        callback(timeRemaining);
    }
    

    It’s easy to use! You specify an end date (as a string, though Date object’s should work as well) and pass in a callback that gets triggered at a set interval. The callback receives a JSON object with properties for days, hours, minutes and seconds. You can pass in a custom interval if you want the callback triggered at a different period. Examples below.


    timer('2011-12-31', function(timeRemaining) {
    	console.log('Timer 1:', timeRemaining);
    });
    
    // This will run every minute, instead of every second
    timer('2012-12-31', function(timeRemaining) {
    	console.log('Timer 2:', timeRemaining);
    }, 60000);
    

    Interested? See it in action: http://jsfiddle.net/mjangda/vGv7J/1/

     
  • Mohammad Jangda 1:01 pm on February 16, 2011 Permalink | Reply
    Tags: , , , javascript errors,   

    JavaScript Tip: Bust and Disable console.log 

    Here’s a quick and dirty follow-up to my original Save me from console.log errors. The main improvement to this version is that it includes a way to disable console.log (and related functions), for example, in production environments.

    While console.log is awesome, you really don’t want your dirty inner workings littering up the Console (or visible to the user — though, you could make an argument that this is a great way to debug errors in live environments) once your app is deployed.

    Note: Firebug can get unhappy sometimes if you try to mess with its console object. But, in theory, this approach should work.


    var DEBUG_MODE = true; // Set this value to false for production
    
    if(typeof(console) === 'undefined') {
        console = {}
    }
    
    if(!DEBUG_MODE || typeof(console.log) === 'undefined') {
        // FYI: Firebug might get cranky...
        console.log = console.error = console.info = console.debug = console.warn = console.trace = console.dir = console.dirxml = console.group = console.groupEnd = console.time = console.timeEnd = console.assert = console.profile = function() {};
    }
    

     
  • Mohammad Jangda 1:54 pm on December 15, 2010 Permalink | Reply
    Tags: css, graphics, images, photoshop, scripts, sprites   

    Link: CSS sprite generator 

    Sweet, sweet CSS

    If you ever find yourself needing to manually build CSS sprites, do yourself a favour and shoot yourself in the head. Then download this Photoshop script and use it.

    Arnau March – CSS sprite generator.

    (via Chris Coyier)

     
  • Mohammad Jangda 1:01 pm on December 10, 2010 Permalink | Reply
    Tags: , js, prototype   

    [[Prototype]] vs prototype (Peter van der Zee on JSMentors) 

    Here’s a great explanation (by Peter van der Zee) of the key differences between [[Prototype]] and prototype in JavaScript. It finally makes sense now!

    Note: to understand this explanation it would help to know what prototype is/does and how it works.

    So we have [[Prototype]] and prototype. You can see prototype as the parent object to which [[Prototype]] (so __proto__) refers to on instances of the constructor. Hah, I’m sure that’s not confusing. So let me give an example :)

    var A = function(){};
    var a = new A();
    log(a instanceof A); // true
    log(A.prototype); // object
    log(a.prototype); // undefined
    log(a.__proto__); // object, in browsers that support the mapping
    log(a.__proto__=== A.prototype); // true.

    “A” has a prototype property (A.prototype). If you add new properties to that object, they will automagically be available on “a”. “a” has a [[Prototype]] internal property. It refers to A.prototype. It is an essential part of the prototypal chain because it actually determines the next part of the chain. If you change a.[[Prototype]], changes to A.prototype will no longer be reflected on “a”. In fact, all not “own” properties of a will no longer be accessible and are replaced by a new set of properties.

    via [JSMentors] Object creation.

     
  • Mohammad Jangda 4:02 pm on October 19, 2010 Permalink | Reply
    Tags: a list apart, survey, websites   

    The Survey For People Who Make Websites, 2010 

    The Survey For People Who Make Websites, 2010

     
  • Mohammad Jangda 12:36 pm on September 18, 2010 Permalink | Reply
    Tags: css3, , mobile, mobileweb, presentation, vortex   

    FITC Mobile 2010 Presentation: HTML5, CSS3, and other fancy buzzwords 

    My presentation at FITC Mobile 2010 went better than I expected! The slides are embedded below and can be downloaded here.

    Note: the animations in the slideshare version don’t work.

    Also, check out some really dumbed down demos I made.

     
  • Mohammad Jangda 9:23 pm on September 13, 2010 Permalink | Reply
    Tags: ajax, callbacks, ,   

    WordPress Tip: Hooking into Widget Save Callback 

    John Gadbois turned some code I gave him into short and useful write-up on how to hook into AJAX calls triggered when saving widgets. It makes use of jQuery’s Global AJAX event handlers, which allow for some very cool things, especially when you’re working with external libraries that use the jQuery AJAX methods. A future blog post will cover how plugins can use the ajaxSend event to hook into WordPress’ autosave.

     
  • Mohammad Jangda 5:23 pm on July 22, 2010 Permalink | Reply
    Tags: , ,   

    jQuery Plugin: HTML5 Inline Data 

    HTML5 Data Attributes are sexy.

    What’s sexier is an easy way to work with these data attributes using jQuery.

    Sure, you could do something like:

    jQuery('#pancakes').attr('data-type');
    

    But who wants to type the “data-” prefix over and over again?

    So, here’s a quick little plugin I whipped up. It’s probably terrible when it comes to jQuery plugin standards (I do WordPress plugins, not jQuery), but it works. It works as a getter and setter.

    Bonus: If you’re using jQuery 1.4.1+, the plugin supports well-formed JSON objects as data attribute values.


    /*
    * Inline Data - get and set HTML5 data attributes! Yay!
    *
    * Copyright (c) 2010 Mohammad Jangda (http://digitalize.ca), Vortex Mobile (http://www.vortexmobile.ca)
    *
    * Licensed under the MIT license:
    * http://www.opensource.org/licenses/mit-license.php
    *
    */
    
    (function($) {
        $.fn.inlinedata = function(name, value) {
    
            var prefix = 'data-';
            var attr = prefix + name;
            var values = []
    
            this.each(function(i) {
                var attrValue;
    
                // Setting values
                if(value) {
                    // If an array is passed, the array index should correspond to the collection index
                    if($.isArray(value)) {
                        // Check, check, check the index. Because out of bounds exceptions are the devil!
                        if(typeof(value[i]) !== 'undefined')
                            attrValue = value[i];
                        else
                            attrValue = '';
                    } else {
                        // Not an array, so we're just giving all the elements the same value
                        attrValue = value;
                    }
                    this.setAttribute(attr, attrValue);
                } else {
                    // Getting values
                    attrValue = this.getAttribute(attr);
    
                    try {
                        // try parsing as JSON
                        attrValue = jQuery.parseJSON(attrValue);
                    } catch(e) { }
                }
                attrValue = (attrValue) ? attrValue : '';
                values.push(attrValue);
            });
    
            if(values.length == 1)
                return values[0]
    
            return values;
        }
    })(jQuery);
    

    Usage is simple:


    
    
    
    
    
    
    
    
    
    
    
    
    

     
  • Mohammad Jangda 3:36 pm on April 29, 2010 Permalink | Reply
    Tags: , , , , ,   

    JavaScript Tip: Save me from console.log errors 

    It’s bound to happen. You build yourself a sweet webapp with some sweet javascript action and you unleash it to the world only to get angry emails yelling, “IT DOESn’T WoRK! FIX iT okAy?” And it’s the darndest problem because it’s happening to both IE and Firefox users (Chrome and Safari users have been silent) and you can’t replicate it.

    And then you spend hours trying to figure out the problem to no avail, leaving you scratching that magnificent head of yours with luscious, flowing hair. You just can’t replicate the problem. But then, after hours and days of staring intently at the screen, you find it. It’s a rogue console.log call that you forgot to comment out. And then you spend the remainder of the week chastising yourself for being stupid.

    It happens. (To be fair, it’s not your fault that your users don’t have Firebug or IE Developer Tools installed. Blame it on Mozilla/Microsoft.)

    But there’s an easy solution. Just copy the following javascript somewhere in your project, and rest easy:


    if(typeof(console) === 'undefined') {
        var console = {}
        console.log = console.error = console.info = console.debug = console.warn = console.trace = console.dir = console.dirxml = console.group = console.groupEnd = console.time = console.timeEnd = console.assert = console.profile = function() {};
    }
    

     
    • Chris Marisic 3:38 pm on March 8, 2011 Permalink | Reply

      Nice article, I just came across this bug affecting orbit jquery slider and posted your article to them https://github.com/zurb/orbit/issues/6 :)

    • Jatinder Assi 3:52 pm on September 27, 2011 Permalink | Reply

      Actually the if stmt will always gets executed in IE 8, even when the developer tools window is open, thus resulting in the console object to be always a nul object.

      Instead here is the solution that I used after modifying your code:

      try
      {
      console.log('test');
      }
      catch(err){
      var console = {};
      console.log = console.error = console.info = console.debug = console.warn = console.trace = console.dir = console.dirxml = console.group = console.groupEnd = console.time = console.timeEnd = console.assert = console.profile = function() {};
      }

  • Mohammad Jangda 12:35 pm on April 6, 2010 Permalink | Reply
    Tags: , html emails, ,   

    WordPress Plugin: HTML Emails 

    Tired of all those boring emails that you get from WordPress? Do you dread opening emails from WordPress because plain text and/or over-querystringified links terrify you? Rest easy friend, HTML emails for WordPress are here.

    HTML Emails replaces the standard WordPress emails with spruced up versions that simply look good. Sample included below:

    New Comment Notification as seen in Gmail

    Currently, only comment notifications are HTML-ized (new comment and comment moderation emails), but I’m hoping to add all other email notifications soon (and yes, that includes Multi-Site emails coming with WordPress 3.0).

    While I have only tested the emails on Gmail, Gmail on Android, and Outlook, they should work on most email clients (including clients without HTML support). If you’re using a client other than the 3 I’ve listed, I would appreciate an email with info on whether the email looks like it should and works correctly.

    Don’t like my design chops? (Go ahead, admit it!) I’ve got you covered! HTML emails makes it easy to fully customize the look and feel of emails sent by WordPress. See the Other Notes section on the Plugin page for details on how to customize emails. More detailed walkthroughs to come.

    Grab it from the Plugin Directory or install directly from WordPress (Plugins > Add New, search for HTML Emails).

     
    • Carlos 1:11 am on April 25, 2010 Permalink | Reply

      Hi, this looks amazing.

      How does it work in WPMU?

      • Mohammad Jangda 9:35 pm on April 29, 2010 Permalink | Reply

        I’m hoping to release a version that works with WordPress 3.0′s Multi-site feature.

    • Nick 5:45 am on May 11, 2010 Permalink | Reply

      Hi Mohammad,

      Great plugin – HTML works fine in all common email clients (Mail, Thunderbird, Outlook Express) on Windows and Mac’s as far as I’ve tested.

      I wonder – is there a way to change the “from” name and email address from wordpress@whateversite.com to a specified email address?

      That would be great.

      Thanks,

      Nick

    • Raoni 10:44 am on May 28, 2010 Permalink | Reply

      Hi,

      I have just translated your plugin into Portuguese Brazil. How can I send you??

      Can you include in the next version???

      And thanks for this GREAT plugin……….

      See ya!

      Raoni

    • Thomas Herold 9:25 pm on July 8, 2010 Permalink | Reply

      Hello Mohammad,

      Any chance to add html email for a user when they request a password reset and also get an email with the new password?

      Thomas

    • Brent Logan 10:27 pm on July 18, 2010 Permalink | Reply

      Nice plugin. Unfortunately, it doesn’t show the commenter’s URL. I need to know that before approving. After all, I wouldn’t approve *any* comment by someone with an URL like: http://nlrgurXXX.com

      • Mo Jangda 12:03 am on July 19, 2010 Permalink | Reply

        Thanks for your comment Brent!

        The commenter’s URL is actually included and linked form their name. But, this has actually thrown me off a few times too and it probably would be easier if the URL was listed separately. I’ll make sure to change that in the next release.

        • Brent Logan 7:22 am on July 19, 2010 Permalink | Reply

          Ha! So it is. That’s nice.

          I’m looking forward to your next release.

    • Sid 5:12 pm on July 22, 2010 Permalink | Reply

      I really liked your plugin . Emails were really sweet as you said instead of those ugly ones of WP.

      If possible add Password notications also in next version please.

      Thanks

    • Grawl 11:41 am on August 18, 2010 Permalink | Reply

      I have just translated in Russian the .pot file from the plugin archive with Poedit in MacOS, but it will show me #$%&@^& errors and cannot open it after saving into the .pot and .po files. But in text mode with any text editor it can be open, so that is translated file: http://cl.ly/716d0df1a6278c0dc052
      Maybe you can make it stable and include my translate into next version.
      Sorry for my bad English :3

    • Nico 9:33 am on September 22, 2010 Permalink | Reply

      Hi!

      Good plugin, I had the same idea but trying to make it work in a different.
      I’m running my WP installation in E_ALL error displaying and have some undefined constants. It’s a shame that there are so many great plugins but auhtors don’t care about notices. The whole WP publishing system runs whithout any notice ! Could you please correct it ? :)

    • LaTonya@ Internet Marketing for Small Businesses 1:55 pm on December 10, 2010 Permalink | Reply

      Ok this might be a really BASIC question, but how do I create an email?

      Stop laughing please;-)

    • cogniter 1:35 am on June 2, 2011 Permalink | Reply

      Any chance to add html email for a user when they request a password reset and also get an email with the new password?

c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
shift + esc
cancel