Dzulqarnain Nasir

  • July 16, 2012

    jQuery Clone vs Template vs HTML String

    Every now and again I get into this situation where I have a list, and some JS function will retrieve more stuff via Ajax to insert into that list. This is normally fine if you stick to normal lists, but once you start using DIVs (eg. grid) and whatnot things start to get a bit more complicated. For one, you will have to make sure the item you’re appending to the list uses the same template as the existing items, for consistency.

    So the question I normally get asked is, “What’s the best way to display the new list items in this kind of situation?”.

    There are a couple of ways to do this, and in most cases they all output the same stuff. The only difference is the speed in which the job gets done.

    Read more »
  • July 10, 2012

    JavaScript performance and ASP.NET

    Today I read Stop paying your jQuery tax by Sam Saffron. This article explains why it’s important to include your JavaScript files at the bottom of the page. It’s a good read, if you’re concerned about JS performance on your sites.

    However, working with ASP.NET, there are times when appending your JS files to the bottom of the page is not an option, especially so when you work with partial views in which your JS functions accepts parameters generated by the application. For example, you have a partial view that accepts some parameters from some other view which is then used in one of your JS functions. But you want to include the jQuery library at the bottom of the page to reduce your initial loading time. However, the function you’ve included in your partial view relies on jQuery being defined before the partial view. So now the script fails because $ or jQuery is undefined when the browser hits it.

    Read more »
  • June 18, 2012

    When not to use the for...in statement

    So today a colleague of mine ran into a small JavaScript problem. One of his functions ran an Ajax request which returns a JSON object which is parsed using jQuery’s parseJSON method. The resulting array is then ran through another function which does some calculations and displays the results in a list.

    Here’s where the problem starts. The method that does the calculations runs through the array using the native JavaScript for...in loop, in which a calculation method is applied to every item in the array. However, for some reason, the list contained one extra item with the value NaN. Upon inspecting the array, I found that there was an extra item called map, which was a function. You can probably already guess by now why the calculation didn’t work as it was expected to, and rightly so, especially when the value used in the calculation was a function.

    Read more »
  • May 15, 2012

    EPiServer CMS: Custom top menu for multiple plugins

    I’ve been working on this small plugin for EPiServer CMS 6, and I wanted to add it to a custom top menu created by another plugin that I had helped develop. I wanted to append the link for my plugin into the same top menu as that other plugin. The only requirement is that my plugin will still display the same top menu even if the other plugin is not installed.

    Unfortunately, after a lot of time Googling and forum crawling, I came to the conclusion that it was just not possible to append an item to a top menu dynamically. I didn’t bother looking at solutions that involved me editing the project’s web.config file, which may have helped, since the other plugin already had some code which adds a top menu item to the top menu bar. So I figured I should probably try to use that instead.

    Read more »
  • May 11, 2012

    Object doesn't support this action - For In Loop and IE

    I’ve recently encountered a problem in Internet Explorer when I was using a for-in loop in one of my scripts. It was a simple loop too.

    for (item in data) {
        $row.data(item, data[item]);
    }
    

    This piece of code will attach a data property for each JSON object it receives from an Ajax request. Simple, right? This works in every other browser I’ve tested it on, EXCEPT Internet Explorer.

    Turned out the word item is a reserved word or something an IE method, because when I swapped item with something else, like j or _item, the code immediately works.

    Read more »