Closing an IE popup two times while busy with Ajax request hangs IE

Closing an IE popup (twice) while a XmlHttpRequest is running, uses up all available connections.

To reproduce this:
- create a slow XmlHttpRequest (xhr). (Running about a minute or so)
- Open a popup window which executes this xhr. Close it while it’s still running
- Open this same popup again. (Second request). Directly close it while the xhr is running.
- Open another popup… et voila… IE Hangs..

This problem is what I was experiencing. After hours of frustrations we found a workaround:
In the onunload of the body tag you must call the “open” method on the same XmlHttpRequest object.
The call of “open” disconnects and stops the old request. Appearently abort doesn’t do this

Working code:

<html>
   <head>
   <script>
       var  xhr = new XMLHttpRequest();  // IE 7 only
       function runXHR() {
           document.body.style.backgroundColor = "red";
           xhr.open("POST", "http://gamecreatures.com/slowquery.php", true);
           xhr.onreadystatechange = function(transport) {
                if (xhr.readyState == 4 && xhr.status == 200 )  {
                     document.body.style.backgroundColor = "green";

                      alert("xhr done!");
            }}
            xhr.send("");
       }

       function closeXHR() {
           //xhr.abort();   // disbled
           // disconnect the connections:

           // !!! This is the solution, an open on the same object, closes the pending connection !!
           xhr.open("POST", "http://gamecreatures.com/whatever.php", true);   
       }
    </script>
   </head>
   <body onunload=“closeXHR()” “>
       <input type=”button” onclick=”runXHR()” value=”Execute XHR” />
   </body>
</html>

WARNING: This solution has only be tested with IE7!

Update 23-01-2008:

Solution for this problem with prototype is. Just put this code in an seperate javascript file. Include this file after prototype.js call iefix_runGarbageControl() in the onunload and the bug is fixed ;-)

// IE TCP/IP Connection Leak FIX, with prototype
if( Prototype.Browser.IE )
{
    var iefix_xhrs = new Array();
    function iefix_runGarbageControl() {
        for(var i=0; i<iefix_xhrs.length; i++) {
            try {
                iefix_xhrs[i].transport.onreadystatechange = Prototype.emptyFunction
                iefix_xhrs[i].transport.open( “GET”, “gamecreature”);
                iefix_xhrs[i] = null;
            } catch( e ) {}
        }       

    // Register responders
    Ajax.Responders.register({
        onCreate: function( xhr ) { iefix_xhrs.push( xhr ); },
        onComplete: function( xhr ) {
            for (var i=0; i<iefix_xhrs.length; i++) {
                if (iefix_xhrs[i] == xhr ) {
                    if (i<iefix_xhrs.length-1) {iefix_xhrs[i] = iefix_xhrs.pop();}
                    break;
                }
            }
        }
    });
}

Update 24-04-2008:
Added: place ‘iefix_runGarbageControl()’ in the onunload

I would like a limit per join in sql!

Currently I’m busy optimizing a query.

I’ve got the following query:

SELECT a.*, c.*
FROM a
LEFT JOIN b ON a.id = b.a_id
LEFT JOIN c ON b.c_id = c.id
WHERE x

I’m only interested in 1 row of the table c. I don’t care for others..
I can only reach table c via table b.
There are many b tables with an a_id.

For performance reasons I would like to implement this like this.

SELECT a.*, c.*
FROM a
LEFT JOIN b ON a.id = b.a_id LIMIT 1
LEFT JOIN c ON b.c_id = c.id
WHERE x

Why isn’t (a form of this contruct) not an sql standard. And hasn’t any database implemented this contruct. Or am I missing something?

When you have many indirect joins this can be a huge time saver.

Btw. Alternative syntax for sql server:

SELECT a.*, c.*
FROM a
LEFT JOIN TOP 1 b ON a.id = b.a_id
LEFT JOIN c ON b.c_id = c.id
WHERE x

** btw The database I need this construct for, has got many redundant records.

Text Measurements with Flash Actionscript 2

Today I found a great class to help me calculating text metrics in Flash. It’s really nice, you can use it to locate every single word / line etc. in a (for instance) wrapping textField. Actionscript 3 has got nice built in methods for this, but unfortunately, for the current project I need to develop with Actionscript 2. Jack Doyle created a very nice actionscript 2 class for this:

http://blog.greensock.com/textmetrics/

PHP Extension order and Core Dumps

After updating my FreeBSD port php and apache I suddenly got a whole lot of core dumps. The hosted websites were running fine, but the core dumps didn’t feel quite right.. (of course).

Another FreeBSD server of mine, also updated to the same version, didn’t have these core dumps.

Doing some research on the web I found that a wrong order in extensions.ini could be a cause of my problems.
Changing the order of the extensions.ini solved my problem!

The following extensions.ini is is working for me:

extension=fileinfo.so
extension=filter.so
extension=json.so
extension=zip.so
extension=hash.so
extension=pdf.so
extension=pgsql.so
extension=ctype.so
extension=mysql.so
extension=mbstring.so
extension=gettext.so
extension=dba.so
extension=sysvshm.so
extension=gmp.so
extension=pdo.so
extension=tidy.so
extension=pcntl.so
extension=openssl.so
extension=readline.so
extension=simplexml.so
extension=calendar.so
extension=posix.so
extension=tokenizer.so
extension=bz2.so
extension=dbase.so
extension=xmlwriter.so
extension=ldap.so
extension=session.so
extension=sybase_ct.so
extension=exif.so
extension=sysvmsg.so
extension=mcrypt.so
extension=bcmath.so
extension=pdo_sqlite.so
extension=mssql.so
extension=sockets.so
extension=zlib.so
extension=pcre.so
extension=curl.so
extension=mhash.so
extension=imap.so
extension=iconv.so
extension=spl.so
extension=dom.so
extension=pspell.so
extension=soap.so
extension=xmlreader.so
extension=shmop.so
extension=sqlite.so
extension=xml.so
extension=xsl.so
extension=mysqli.so
extension=wddx.so
extension=sysvsem.so
extension=ftp.so
extension=xmlrpc.so
extension=snmp.so
extension=ncurses.so
extension=odbc.so
extension=ming.so
extension=gd.so

Ruby’s “Begin Rescue” and “Try and Catch”

Thanks to the book “Programming Language Pragmatics”
Programming Language Pragmatics
I’m beginning to appreciate Ruby’s different notation for exception handling.

A sample:

begin
  value = 7 / 0
rescue
  print “Something went wrong! This is an error”
end

The code above is to catch an exception. (In Java or C++ you would use try and catch)

Ruby also has got try and catch, but this isn’t used for exception handling. It is used for implementing “Multi Level Returns”.
A sample:

def search( name )
  # .. some fancy code here ..
  if filename.contains( name ) throw :found_it, filename
end

result = catch :found_it do
  search “one”
  search “two”
  search “three”
  false
end

In the sample above, result will be filled with the found filename. This can be
with the keyword “one”, “two” or “three”. If nothing is found result is filled with false. (the last value in the catch block)

I don’t think I need the catch construct very much, but it’s a nice feature!
Btw. I recommend the book “Programming Language Pragmatics”.

« Previous PageNext Page »