rajivp
11-25-2009, 04:00 AM
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.
function getMultipleSelection(mSelect){
var delim = "";
var selParams = "";
for(j = 0; j < mSelect.options.length; j++) {
if(mSelect.options[j].selected) {
selParams += delim + mSelect.options[j].value;
delim = ":";
}
}
return selParams;
}
function create_request_string(theform) {
var reqStr = "";
for(i=0; i < theform.elements.length; i++) {
isformObject = false;
switch (theform.elements[i].tagName) {
case "INPUT":
switch (theform.elements[i].type) {
case "FILE":
case "text":
case "password":
case "hidden":
reqStr += theform.elements[i].name + "=" + encodeURIComponent(theform.elements[i].value);
isformObject = true;
break;
case "checkbox":
if (theform.elements[i].checked) {
reqStr += theform.elements[i].name + "=" + theform.elements[i].value;
}
else {
reqStr += theform.elements[i].name + "=";
}
isformObject = true;
break;
case "radio":
if (theform.elements[i].checked) {
reqStr += theform.elements[i].name + "=" + theform.elements[i].value;
isformObject = true;
}
}
break;
case "TEXTAREA":
reqStr += theform.elements[i].name + "=" + encodeURIComponent(theform.elements[i].value);
isformObject = true;
break;
case "SELECT":
var sel = theform.elements[i];
if (sel.multiple) {
var mSelOptions = getMultipleSelection (sel);
reqStr += sel.name + "=" + mSelOptions;
}
else {
reqStr += sel.name + "=" + sel.options[sel.selectedIndex].value;
}
isformObject = true;
break;
}
if ((isformObject) && ((i+1)!= theform.elements.length)) {
reqStr += "&";
}
}
return reqStr;
}
//Browser Support Code
function populateMainContent (method, form, url, params){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
document.getElementById('MainContentInner').innerHTML = "<div style=\"font-size: 14px;font-weight:bold;margin-top:80px;\"><img src=\"images/loading.gif\"></div>";
ajaxRequest.open(method, url, true);
if (method == 'POST') {
var reqStr = create_request_string(form);
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.setRequestHeader("Content-length", reqStr.length);
ajaxRequest.setRequestHeader("Connection", "close");
ajaxRequest.onreadystatechange = function() {//Call a function when the state changes.
if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
document.getElementById('MainContentInner').innerHTML = ajaxRequest.responseText;
}
}
ajaxRequest.send(reqStr);
}
else {
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.getElementById('MainContentInner').innerHTML = ajaxRequest.responseText;
}
}
ajaxRequest.send(null);
}
return true;
}
Sample Response text
<form name="AlbumDelete" method="POST" onsubmit="populateMainContent('POST', this, 'http://www.xx.com/cgi-bin/deleteAlbum.pl', ''); return false;">
<input type="hidden" name="AdID" value="1234">
<div style="text-align:left;width:500px;font-weight: bold;margin-bottom: 10px;">Delete Photos from Album</b></div>
<div style="text-align:left;width:500px;margin-bottom: 10px;">
Select the Photos you want to delete and press Submit
</div>
<div style="text-align:left;width:500px;margin-bottom: 15px;border-bottom: 1px dotted silver;padding-bottom: 5px;">
<img src="http://www.xx.com/img.jpg">
<input type=checkbox name=1> Delete This Photo?
</div>
<div style="text-align:left;width:500px;margin-bottom: 15px;border-bottom: 1px dotted silver;padding-bottom: 5px;">
<img src="http://www.xx.com/img.jpg">
<input type=checkbox name=2> Delete This Photo?
</div>
<div style="text-align:left;width:500px;margin-bottom: 15px;border-bottom: 1px dotted silver;padding-bottom: 5px;">
<img src="http://www.xx.com/img.jpg">
<input type=checkbox name=3> Delete This Photo?
</div>
<div style="text-align:left;width:500px;">
<input type="submit" name="Submit" value="Submit">
</div>
</form>
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.
function getMultipleSelection(mSelect){
var delim = "";
var selParams = "";
for(j = 0; j < mSelect.options.length; j++) {
if(mSelect.options[j].selected) {
selParams += delim + mSelect.options[j].value;
delim = ":";
}
}
return selParams;
}
function create_request_string(theform) {
var reqStr = "";
for(i=0; i < theform.elements.length; i++) {
isformObject = false;
switch (theform.elements[i].tagName) {
case "INPUT":
switch (theform.elements[i].type) {
case "FILE":
case "text":
case "password":
case "hidden":
reqStr += theform.elements[i].name + "=" + encodeURIComponent(theform.elements[i].value);
isformObject = true;
break;
case "checkbox":
if (theform.elements[i].checked) {
reqStr += theform.elements[i].name + "=" + theform.elements[i].value;
}
else {
reqStr += theform.elements[i].name + "=";
}
isformObject = true;
break;
case "radio":
if (theform.elements[i].checked) {
reqStr += theform.elements[i].name + "=" + theform.elements[i].value;
isformObject = true;
}
}
break;
case "TEXTAREA":
reqStr += theform.elements[i].name + "=" + encodeURIComponent(theform.elements[i].value);
isformObject = true;
break;
case "SELECT":
var sel = theform.elements[i];
if (sel.multiple) {
var mSelOptions = getMultipleSelection (sel);
reqStr += sel.name + "=" + mSelOptions;
}
else {
reqStr += sel.name + "=" + sel.options[sel.selectedIndex].value;
}
isformObject = true;
break;
}
if ((isformObject) && ((i+1)!= theform.elements.length)) {
reqStr += "&";
}
}
return reqStr;
}
//Browser Support Code
function populateMainContent (method, form, url, params){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
document.getElementById('MainContentInner').innerHTML = "<div style=\"font-size: 14px;font-weight:bold;margin-top:80px;\"><img src=\"images/loading.gif\"></div>";
ajaxRequest.open(method, url, true);
if (method == 'POST') {
var reqStr = create_request_string(form);
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.setRequestHeader("Content-length", reqStr.length);
ajaxRequest.setRequestHeader("Connection", "close");
ajaxRequest.onreadystatechange = function() {//Call a function when the state changes.
if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
document.getElementById('MainContentInner').innerHTML = ajaxRequest.responseText;
}
}
ajaxRequest.send(reqStr);
}
else {
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.getElementById('MainContentInner').innerHTML = ajaxRequest.responseText;
}
}
ajaxRequest.send(null);
}
return true;
}
Sample Response text
<form name="AlbumDelete" method="POST" onsubmit="populateMainContent('POST', this, 'http://www.xx.com/cgi-bin/deleteAlbum.pl', ''); return false;">
<input type="hidden" name="AdID" value="1234">
<div style="text-align:left;width:500px;font-weight: bold;margin-bottom: 10px;">Delete Photos from Album</b></div>
<div style="text-align:left;width:500px;margin-bottom: 10px;">
Select the Photos you want to delete and press Submit
</div>
<div style="text-align:left;width:500px;margin-bottom: 15px;border-bottom: 1px dotted silver;padding-bottom: 5px;">
<img src="http://www.xx.com/img.jpg">
<input type=checkbox name=1> Delete This Photo?
</div>
<div style="text-align:left;width:500px;margin-bottom: 15px;border-bottom: 1px dotted silver;padding-bottom: 5px;">
<img src="http://www.xx.com/img.jpg">
<input type=checkbox name=2> Delete This Photo?
</div>
<div style="text-align:left;width:500px;margin-bottom: 15px;border-bottom: 1px dotted silver;padding-bottom: 5px;">
<img src="http://www.xx.com/img.jpg">
<input type=checkbox name=3> Delete This Photo?
</div>
<div style="text-align:left;width:500px;">
<input type="submit" name="Submit" value="Submit">
</div>
</form>