Chad Scharf's Weblog
It's always time to upgrade!

Shrinking JavaScript Arrays

Friday, 11 April 2008 20:21 by Chad

Recently I've found myself writing a ton of client code in a new pure JavaScript/WCF project I am working on. Since most of the programming I'm doing for the UI is in JavaScript files, I've found myself having to work with the Array object for many different things. The one thing I've noticed however, is that you cannot remove an item from a JavaScript array, say, in the middle of the array and have the array size shrink without losing data at either end, however having to check for array_name[i] != null every single time I iterate the array was getting annoying, to say the least.

I decided to flex the muscle of the prototype, which in JavaScript allows you to extend a types method and properties. This new method I created, called shrink(), aptly removes all of the nulls from my array and shrinks the total length without losing any data. I'm not sure how efficient it is, but it works and I no longer have to check for null where at least it doesn't make sense to anyway.

Array.prototype.shrink = function() {
    var sync = new Array();
    while (this.length > 0) {
        var obj = this.shift();
        if (obj !== null) sync.push(obj);
    }
    while (sync.length > 0) {
        var obj = sync.shift();
        if (obj !== null) this.push(obj);
    }
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:  
Categories:   The Client Side
Actions:   E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Protecting PDF files in IIS 6 using Forms Authentication

Thursday, 3 April 2008 23:32 by Chad

Problem

We have a very simple ASP.NET web site that uses the built in Forms Authentication provider out of the box for users, roles and security. Our internal business customers have hundreds of PDF documents that should only be accessible for specific users or roles within the company. A majority of users that need access to these files are outside the company firewall and do not have VPN access. The site must be hosted on Windows Server 2003 using IIS 6.

Although this problem could be solved in many ways, I wanted to make the site as dynamic as possible since our web master would need to make constant changes to files, structure, etc and we did not want a programmer having to make changes to specific code or a DBA maintaining a database of files, paths, etc. We also wanted to avoid using a network file share to host the files or wrap the site using Plain Text authentication. More...

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5