Your browser is very old. You might enjoy surfing the web more if you used something newer like:

Google Chrome

Even Firefox would be OK.

If you're being forced at gunpoint to use Internet Explorer, you should at least upgrade it. Version 8 is tolerable and 9 will be OK when it comes out.

Posts tagged “javascript”

Can’t load XRegExp twice in the same frame

Have you ever gotten this error when using jQuery to handle DOM events?

Can’t load XRegExp twice in the same frame

In many cases, I’ve found that to be caused by code like this:

$(‘a.foo’).click( function() {
  alert(‘You clicked on me!’);
});

when a.foo doesn’t exist in the DOM yet.

The fix? Use jQuery.live() to bind to the event.

$(‘a.foo’).live( ‘click’, function() {
  alert(‘You clicked on me!’);
});

I’ve also run into a couple of situations where Chrome Extensions for formatting JSON & XML also caused this error in some cases. Disabling those extensions also fixed the error.

Thanks to this Stackoverflow answer for the suggestion.

Basic Authentication with Titanium.Network.HTTPClient

I’ve been trying, over the last couple of days, to figure out how to do an HTTP request with Basic Authentication using Appcelerator Titanium.

It seemed like it should be pretty easy. Just create an HTTPClient object, pass it the username and password via setBasicCredentials(), and off you go.

But it didn’t work. I got 401 – Unauthorized on every single site I tried.

Finally, after way too much research, I discovered that setBasicCredentials just doesn’t work and you have to do Basic Authentication by setting the headers yourself like this:

authstr = 'Basic ' +Titanium.Utils.base64encode(username+':'+password);
xhr.setRequestHeader('Authorization', authstr);

It’s important to note that setRequestHeader() has to be called after you’ve called open() but before you call send()

And here’s a complete function for making HTTP requests.

var httpRequest = function(params) {
    var xhr = Titanium.Network.createHTTPClient();

    xhr.onload = function() {
        if(params.hasOwnProperty('callback')) {
            if(typeof params.callback == 'function') {
                params.callback(this.responseText);
            }else {
                Ti.API.error('getXML:  Invalid callback function');
            }
        }
    };
    xhr.onerror = function() {
        Ti.API.error(this.status + ' - ' + this.statusText);
    };

    xhr.open( params.hasOwnProperty('method') ? params.method : 'GET', params.url);

    try {
        if(params.hasOwnProperty('username') && params.hasOwnProperty('password')) {
            authstr = 'Basic ' +Titanium.Utils.base64encode(params.username+':'+params.password);
            xhr.setRequestHeader('Authorization', authstr);
        }
    }catch(e) {
        Ti.API.error('Error in getXML:  ' + e.message);
    }

    xhr.send();
};

//Call it like this
httpRequest( {
        url:  'https://api.del.icio.us/v1/tags/get',
        username: 'username',
        password:  'password',
        callback:  function(resp) {
            //TODO
        }
    });

Even better overriding the Firebug console in IE

I wrote, a while back, about Overriding the Firebug console object to prevent IE from throwing errors when you console.log all over your code.

The only problem that first version is that it’s too much to type. Here’s a much shorter way of doing it using object literal notation.

if(typeof console=='undefined'){console={log:function(){}};}

It’d be easy to enough add a handler for the console.debug function and to have those functions actually do something in IE (of course you could also just use Firebug Lite) but I find the snippet above is good enough for most cases.