I have this web page where I have a menu bar at the top and underneath the menu bar I have a div element with id="MainContentInner" - When a menu item is clicked on I call Ajax script to replace the contents of MainContentInner by doing something like the following

MainContentInner.innerHTML = response text

Lot of times, the response text is an HTML form. The forms are displayed fine every time menu items are clicked on. But when the submit button is pressed, it works in Firefox, but not in IE. When the javascript is called, in IE the form.element.length returns 0 (it is fine in Firefox)

If the form is part of the the page when the page was first displayed, it works fine in IE. What does not work is when a form is brought in to the page with an innerHTML assignment. This does not work with IE.

I am not too familiar with Javascript or AJAX. I have used sample code that I found on the web. I have given the javascript code as well as a sample response text. I would appreciate your help.

What does not work is when a form is brought in to the page with an innerHTML assignment. This does not work with IE.
innerHTML is not a standard DOM method, it will not really insert all the elements nor their attributes into the DOM tree. I would say that you should use DOM methods to create and append the elements (createElement(), setAttribute() appendChild()...and so on). And take care about the differences between IE and other browsers.

I am not too familiar with Javascript or AJAX

Try to.

rajivp

11-27-2009, 05:31 PM

Here is the situation though. Everywhere I read (including the Javascript/AJAX books from O'Reilley), there is a debate on whether to use innerHTML or not. And then they go on to use innerHTML instead of the DOM methods everywhere. They do not recommend not using it. Granted, innerHTML is not standand, but it is used in far too many places.

When you have a very large form, the DOM method would be very involved compared to the one liner with innerHTML. The irony is innerHTML is Microsoft proprietory and it does NOT work in IE.

Unfortunately, right now, I do not have the time to tinker with DOM methods. I ended up removing ajax from the forms and I made the forms simple popups. For my purpose that works just as good.

If you are a newbie like me, you will learn the hard way that ajax is not always an easy solution. If you have the time, read up and design carefully.

Kor

11-27-2009, 07:01 PM

As I said: innerHTML is not a standard DOM method, even if it is crossbrowser and really helpful. Even the Microsoft team recognize some of its limits:
http://msdn.microsoft.com/en-us/library/ms533897%28VS.85%29.aspx

By short: if you intend only to display some elements, innerHTML will do the job. But, if later you want to handle the new inserted elements on using DOM reference methods, some problems, sometimes but not always, might occur, depending on the browser. The only thing you may do is to test, as a first choice, the innerHTML, and check if what you need works. If not, it is time to use the more elaborate but more safer DOM methods. That means also to organize the server-side response data in a JSON (http://json.org) manner (or XML).

rajivp

11-28-2009, 04:24 AM

Thanks for the insight.

I am using ajax for display currently. I will go back and revisit the issues when I have some time.