The Problem

Adding things into the DOM in IE8 was going horribly wrong. Updating a segment of code after an AJAX post request was then breaking any subsequent requests.
After many hours trying to figure out what the problem could possibly be, it became apparent that the form action attribute was to blame.

The Reason

 

It turns out that when IE8 inserts HTML into the DOM using the html() function, it likes to mangle perfectly good HTML. The form element was getting its attribute rearranged so that the action attribute came last in the ordering. Not only this but the quotation marks were also being stripped. The URL in the action attribute had a trailing slash, as required when doing POST requests* via forms. All of this together was causing IE8 to see:

 

<form ...snip... action=/some/path/ >

Rather than assume the trailing slash belonging to the URL it was closing the form prematurely.

(Thanks to http://richardmiller.co.uk/2011/03/11/jquery-ie8-bug-when-adding-a-form/ for a good explanation of this).

The Solution

Don’t use the html() function. Turning

$("#element_id").html($some_html);

into

$("#element_id")[0].innerHTML = $some_html[0].innerHTML;

works just fine. In fact, doing anything to prevent the action attribute from being put at the end of the form element will do the trick.

* Leaving off the trailing slash causes most webservers to do a re-direct. POST requests are not supposed to follow redirects without confirmation from the client. Sure, sometimes browsers will do this for you and there will be no problem but it’s better to just do it properly in the first place and avoid the second hit on the webserver.

Search

Bad implementation always trumps good ideas

19 September 2015 11:34 a.m.

Good practice in software development has always preached loose coupling and high cohesion.

While most developers understand this and try ...

Read More

New platform, new challenges

13 September 2015 19:13 p.m.

There was a question raised by a collegue of mine recently, that asked "How long should it take to get ...

Read More

SaltStack and Vagrant

14 February 2013 15:14 p.m.

Salt is an open source infrastructure management tool. While one use is to manage thousands of servers, it’s also very ...

Read More