CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2000
    Posts
    129

    Ajax seems to kill IE

    I have a simple 2 way ajax chat system. Basically it's to allow someone to have 1 on 1 conversations with multiple people. It works fine in FF and Opera but for some reason in IE after a period of time, about 20-30 minutes, IE will stop loading the page. You can't even browse to the page any more. The only way to fix the issue is to restart IE.

    I can't figure out why. It's like the pipe I make to the server do not close and keep recreating themselves over and over until IE fails.

    Code:
    var updateInterval = 1000;
    var users_content;
    var chat_text;
    var chat_content;
    var buttons;
    var xmlHttpGetUsers = createXmlHttpRequestObject();
    var xmlHttpGetChats = createXmlHttpRequestObject();
    var xmlHttpGetButtons = createXmlHttpRequestObject();
    var xmlHttpGetSendMessage = createXmlHttpRequestObject();
    var timer_chats;
    var isDown = false;
    
    function createXmlHttpRequestObject() {
    	// will store the reference to the XMLHttpRequest object
    	var xmlHttp;
    	// this should work for all browsers except IE6 and older
    	try {
    		// try to create XMLHttpRequest object
    		xmlHttp = new XMLHttpRequest();
    	} catch(e) {
    		// assume IE6 or older
    		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
    										"MSXML2.XMLHTTP.5.0",
    										"MSXML2.XMLHTTP.4.0",
    										"MSXML2.XMLHTTP.3.0",
    										"MSXML2.XMLHTTP",
    										"Microsoft.XMLHTTP");
    		// try every prog id until one works
    		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {
    			try {
    				// try to create XMLHttpRequest object
    				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
    			} catch (e) {}
    		}
    	}
    	// return the created object or display an error message
    	if (!xmlHttp) {
    		alert("Error creating the XMLHttpRequest object.");
    	} else {
    		return xmlHttp;
    	}
    }
    
    /*=== Users ====*/
    function requestUsers() {
    	//Random number of IE
    	var ran_number = Math.random()*5;
    	//URL to go to
    	var url = 'get_users.php?my='+my+'&type='+u_type+'&get_users=1&trash='+ran_number;
    
    	if(xmlHttpGetUsers) {
    		try {
    			// don't start another server operation if such an operation
    			//   is already in progress
    			if (xmlHttpGetUsers.readyState == 4 || xmlHttpGetUsers.readyState == 0) {
    				// call the server page to execute the server-side operation
    				xmlHttpGetUsers.open("GET", url, true);
    				xmlHttpGetUsers.onreadystatechange = handleReceivingUsers;
    				xmlHttpGetUsers.send(null);
    			} else {
    				// we will check again for new users
    				setTimeout("requestUsers();", updateInterval);
    			}
    		} catch(e) {
    			displayError(e.toString());
    		}
    	}
    }
    
    function handleReceivingUsers() {
    	// continue if the process is completed
    	if (xmlHttpGetUsers.readyState == 4) {
    		// continue only if HTTP status is "OK"
    		if (xmlHttpGetUsers.status == 200) {
    			try {
    				// process the server's response
    				readUsers();
    			} catch(e) {
    				// display the error message
    				displayError(e.toString());
    			}
    		} else {
    			// display the error message
    			displayError(xmlHttpGetUsers.statusText);
    		}
    	}
    }
    
    function readUsers() {
    	// retrieve the server's response
    	var response = xmlHttpGetUsers.responseText;
    	//Clear the users
    	users_content.innerHTML = '';
    
    	//If there is an error
    	if (response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 )  displayError('Sever Error '+response);
    	//If there is no data
    	if (response.length == 0) {
    		setTimeout("requestUsers();", updateInterval);
    		return;
    	}
    
    	//Set the users HTML to the response
    	users_content.innerHTML = response;
    	//Reset the timer
    	setTimeout("requestUsers();", updateInterval);
    }
    
    /*=== Chats ===*/
    function requestChats() {
    	//Random number of IE
    	var ran_number = Math.random()*5;
    	//URL to go to
    	var url = 'chat.php?last_chat='+last_chat+'&sid='+chat_id+'&type='+u_type+'&my='+my+'&trash='+ran_number;
    
    	if(xmlHttpGetChats) {
    		try {
    			// don't start another server operation if such an operation
    			//   is already in progress
    			if (xmlHttpGetChats.readyState == 4 || xmlHttpGetChats.readyState == 0) {
    				// call the server page to execute the server-side operation
    				xmlHttpGetChats.open("GET", url, true);
    				xmlHttpGetChats.onreadystatechange = handleReceivingChats;
    				xmlHttpGetChats.send(null);
    			} else {
    				// we will check again for new messages
    				timer_chats = setTimeout("requestChats();", updateInterval);
    			}
    		} catch(e) {
    			displayError(e.toString());
    		}
    	}
    }
    
    function handleReceivingChats() {
    	// continue if the process is completed
    	if (xmlHttpGetChats.readyState == 4) {
    		// continue only if HTTP status is "OK"
    		if (xmlHttpGetChats.status == 200) {
    			try {
    				// process the server's response
    				readChats();
    			} catch(e) {
    				// display the error message
    				displayError(e.toString());
    			}
    		} else {
    			// display the error message
    			displayError(xmlHttpGetChats.statusText);
    		}
    	}
    }
    
    function readChats() {
    	// retrieve the server's response
    	var response = xmlHttpGetChats.responseText;
    
    	//Split the response
    	var ret = new Array(2);
    	ret = get_last_chat(response);
    	response = ret[0];
    	last_chat = ret[1];
    
    	if (response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 )  displayError('Sever Error '+response);
    	if (response.length <= 1) {
    		timer_chats = setTimeout("requestChats();", updateInterval);
    		return;
    	}
    
    	chat_content.innerHTML = chat_content.innerHTML + response;
    	chat_content.scrollTop = chat_content.scrollHeight;
    	timer_chats = setTimeout("requestChats();", updateInterval);
    }
    
    /*=== Send Chat ===*/
    function send_chat_text() {
    	//Random number of IE
    	var ran_number = Math.random()*5;
    	//URL to go to
    	//We encode it in base 64 just to make sure that nothing gets lost in translation
    	var url = 'chat.php?my='+my+'&type='+u_type+'&text='+ encode(chat_text.value) +'&chat_id=' +chat_id+'&trash='+ran_number;
    
    	//Clear the text area
    	chat_text.value ='';
    	chat_text.focus();
    	//Send the command
    	if(xmlHttpGetSendMessage) {
    		try {
    			// don't start another server operation if such an operation
    			//   is already in progress
    			if (xmlHttpGetSendMessage.readyState == 4 || xmlHttpGetSendMessage.readyState == 0) {
    				// call the server page to execute the server-side operation
    				xmlHttpGetSendMessage.open("GET", url, true);
    				xmlHttpGetSendMessage.send(null);
    			}
    		} catch(e) {
    			displayError(e.toString());
    		}
    	}
    }
    
    /*=== Buttons ===*/
    function requestButtons() {
    	var ran_number = Math.random()*10;
    	var url = 'get_buttons.php?sid='+chat_id+'&my='+my+'&type='+u_type+'&trash='+ran_number;
    
    	// only continue if xmlHttpGetMessages isn't void
    	if(xmlHttpGetButtons) {
    		try {
    			// don't start another server operation if such an operation
    			//   is already in progress
    			if (xmlHttpGetButtons.readyState == 4 || xmlHttpGetButtons.readyState == 0) {
    				// call the server page to execute the server-side operation
    				xmlHttpGetButtons.open("GET", url, true);
    				xmlHttpGetButtons.onreadystatechange = handleReceivingButtons;
    				xmlHttpGetButtons.send(null);
    			}
    		} catch(e) {
    			displayError(e.toString());
    		}
    	}
    }
    
    function handleReceivingButtons() {
    	// continue if the process is completed
    	if (xmlHttpGetButtons.readyState == 4) {
    		// continue only if HTTP status is "OK"
    		if (xmlHttpGetButtons.status == 200) {
    			try {
    				// process the server's response
    				readButtons();
    			} catch(e) {
    				// display the error message
    				displayError(e.toString());
    			}
    		} else {
    			// display the error message
    			displayError(xmlHttpGetButtons.statusText);
    		}
    	}
    }
    
    function readButtons() {
    	// retrieve the server's response
    	var response = xmlHttpGetButtons.responseText;
    
    	if (response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 )  displayError('Sever Error '+response);
    	if (response.length == 0) {
    		return;
    	}
    
    	buttons.innerHTML = response;
    }
    
    /*=== Other Functions ===*/
    
    // function that displays an error message
    function displayError(message) {
    	alert("Error accessing the server! "+ "\r\n" + message);
    }
    
    function get_last_chat(Request) {
    	var ret = new Array(2);
    	loc = Request.indexOf('<last_chat>');
    	loc2 = Request.indexOf('</last_chat>');
    	if ((loc-1) <= 0) {
    		ret[0] = '';
    	} else {
    		ret[0] = Request.substring(0, loc-1);
    	}
    	ret[1] = Request.substring(loc+11, loc2);
    	return ret;
    }
    
    function keydown(e) {
    	var characterCode; //literal character code will be stored in this variable
    	e = (!e) ? window.event : e;
    
    	characterCode = (e.charCode) ? e.charCode :
    		 ((e.keyCode) ? e.keyCode :
    		 ((e.which) ? e.which : 0));
    
    	//If Shift
    	if (characterCode == 16) {
    		isDown = true;
    	}
    	//If CTRL
    	if (characterCode == 17) {
    		isDown = true;
    	}
    
    }
    function checkEnter(e){ //e is event object passed from function invocation
    	var characterCode; //literal character code will be stored in this variable
    	e = (!e) ? window.event : e;
    
    	characterCode = (e.charCode) ? e.charCode :
    		 ((e.keyCode) ? e.keyCode :
    		 ((e.which) ? e.which : 0));
    
    	if(characterCode == 13 && (isDown == null || !isDown)){ //if generated character code is equal to ascii 13 (if enter key)
    		send_chat_text(); //submit the form
    		return false;
    	} else {
    		isDown = false;
    		return true;
    	}
    }
    
    function change_chat(chatid) {
    	clearTimeout(timer_chats);
    	//Add the chat just sent
    	chat_content.innerHTML = '';
    	//Set focus back to it
    	chat_text.focus();
    	chat_id = chatid;
    	last_chat = 0;
    	requestButtons();
    	requestChats();
    }
    
    function loader() {
    	users_content = xGetElementById("users");
    	chat_text = xGetElementById("chat_text");
    	chat_content = xGetElementById("chat_content");
    	buttons = xGetElementById("chat_buttons");
    	requestUsers();
    	requestChats();
    	chat_text.focus();
    }
    
    if (window.addEventListener) {
    	window.addEventListener("load", loader, false);
    } else if (window.attachEvent) {
    	window.attachEvent("onload", loader);
    }
    Any ideas?

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Ajax seems to kill IE

    You might be causing a memory leak. Check process manager and see with how much memory you are working. Watch it for some time and see if it just keeps increasing.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Dec 2000
    Posts
    129

    Re: Ajax seems to kill IE

    Seems to not be the case as IE memory actually counts down over time, untouched.

    Updated code:
    Code:
    var updateInterval = 1000;
    var users_content;
    var chat_text;
    var chat_content;
    var buttons;
    var xmlHttpGetUsers = createXmlHttpRequestObject();
    var xmlHttpGetChats = createXmlHttpRequestObject();
    var xmlHttpGetButtons = createXmlHttpRequestObject();
    var xmlHttpGetSendMessage = createXmlHttpRequestObject();
    var timer_chats;
    var timer_users;
    var isDown = false;
    var newwindow = "";
    var detach = false;
    
    function createXmlHttpRequestObject() {
    	// will store the reference to the XMLHttpRequest object
    	var xmlHttp;
    	// this should work for all browsers except IE6 and older
    	try {
    		// try to create XMLHttpRequest object
    		xmlHttp = new XMLHttpRequest();
    	} catch(e) {
    		// assume IE6 or older
    		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
    										"MSXML2.XMLHTTP.5.0",
    										"MSXML2.XMLHTTP.4.0",
    										"MSXML2.XMLHTTP.3.0",
    										"MSXML2.XMLHTTP",
    										"Microsoft.XMLHTTP");
    		// try every prog id until one works
    		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {
    			try {
    				// try to create XMLHttpRequest object
    				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
    			} catch (e) {}
    		}
    	}
    	// return the created object or display an error message
    	if (!xmlHttp) {
    		alert("Error creating the XMLHttpRequest object.");
    	} else {
    		return xmlHttp;
    	}
    }
    
    /*=== Users ====*/
    function requestUsers() {
    	//Random number of IE
    	var ran_number = Math.random()*5;
    	//URL to go to
    	var url = 'get_users.php?my='+my+'&type='+u_type+'&get_users=1&trash='+ran_number;
    
    	if(xmlHttpGetUsers) {
    		try {
    			// don't start another server operation if such an operation
    			//   is already in progress
    			if (xmlHttpGetUsers.readyState == 4 || xmlHttpGetUsers.readyState == 0) {
    				// call the server page to execute the server-side operation
    				xmlHttpGetUsers.open("GET", url, true);
    				xmlHttpGetUsers.onreadystatechange = handleReceivingUsers;
    				xmlHttpGetUsers.send(null);
    			} else {
    				if (timer_users) {
    					clearTimeout(timer_users);
    					timer_users = null;
    				}
    				// we will check again for new users
    				timer_users = setTimeout("requestUsers();", updateInterval);
    			}
    		} catch(e) {
    			displayError(e.description);
    		}
    	}
    }
    
    function handleReceivingUsers() {
    	// continue if the process is completed
    	if (xmlHttpGetUsers.readyState == 4) {
    		// continue only if HTTP status is "OK"
    		if (xmlHttpGetUsers.status == 200) {
    			try {
    				// process the server's response
    				readUsers();
    			} catch(e) {
    				// display the error message
    				displayError(e.description);
    			}
    		} else {
    			// display the error message
    			displayError(xmlHttpGetUsers.statusText);
    		}
    	}
    }
    
    function readUsers() {
    	// retrieve the server's response
    	var response = xmlHttpGetUsers.responseText;
    	//Clear the users
    	users_content.innerHTML = '';
    
    	//If there is an error
    	if (response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 )  displayError('Sever Error '+response);
    	//If there is no data
    	if (response.length > 2) {
    		//Set the users HTML to the response
    		users_content.innerHTML = response;
    	}
    
    	//Reset the timer
    	if (timer_users) {
    		clearTimeout(timer_users);
    		timer_users = null;
    	}
    	timer_users = setTimeout("requestUsers();", updateInterval);
    }
    
    /*=== Chats ===*/
    function requestChats() {
    	//Random number of IE
    	var ran_number = Math.random()*5;
    	//URL to go to
    	var url = 'chat.php?last_chat='+last_chat+'&sid='+chat_id+'&type='+u_type+'&my='+my+'&trash='+ran_number;
    
    	if(xmlHttpGetChats) {
    		try {
    			// don't start another server operation if such an operation
    			//   is already in progress
    			if (xmlHttpGetChats.readyState == 4 || xmlHttpGetChats.readyState == 0) {
    				// call the server page to execute the server-side operation
    				xmlHttpGetChats.open("GET", url, true);
    				xmlHttpGetChats.onreadystatechange = handleReceivingChats;
    				xmlHttpGetChats.send(null);
    			} else {
    				if (timer_chats) {
    					clearTimeout(timer_chats);
    					timer_chats = null;
    				}
    				// we will check again for new messages
    				timer_chats = setTimeout("requestChats();", updateInterval);
    			}
    		} catch(e) {
    			displayError(e.description);
    		}
    	}
    }
    
    function handleReceivingChats() {
    	// continue if the process is completed
    	if (xmlHttpGetChats.readyState == 4) {
    		// continue only if HTTP status is "OK"
    		if (xmlHttpGetChats.status == 200) {
    			try {
    				// process the server's response
    				readChats();
    			} catch(e) {
    				// display the error message
    				displayError(e.description);
    			}
    		} else {
    			// display the error message
    			displayError(xmlHttpGetChats.statusText);
    		}
    	}
    }
    
    function readChats() {
    	// retrieve the server's response
    	var response = xmlHttpGetChats.responseText;
    
    	//Split the response
    	var ret = new Array(2);
    	ret = get_last_chat(response);
    	response = ret[0];
    	last_chat = ret[1];
    
    	if (response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 )  displayError('Sever Error '+response);
    	if (response.length > 2) {
    		chat_content.innerHTML = chat_content.innerHTML + response;
    		chat_content.scrollTop = chat_content.scrollHeight;
    	}
    
    	if (timer_chats) {
    		clearTimeout(timer_chats);
    		timer_chats = null;
    	}
    	timer_chats = setTimeout("requestChats();", updateInterval);
    }
    
    /*=== Send Chat ===*/
    function send_chat_text() {
    	//Random number of IE
    	var ran_number = Math.random()*5;
    	//URL to go to
    	//We encode it in base 64 just to make sure that nothing gets lost in translation
    	var url = 'chat.php?my='+my+'&type='+u_type+'&text='+ encode(chat_text.value) +'&chat_id=' +chat_id+'&trash='+ran_number;
    
    	//Clear the text area
    	chat_text.value ='';
    	chat_text.focus();
    	//Send the command
    	if(xmlHttpGetSendMessage) {
    		try {
    			// don't start another server operation if such an operation
    			//   is already in progress
    			if (xmlHttpGetSendMessage.readyState == 4 || xmlHttpGetSendMessage.readyState == 0) {
    				// call the server page to execute the server-side operation
    				xmlHttpGetSendMessage.open("GET", url, true);
    				xmlHttpGetSendMessage.send(null);
    			}
    		} catch(e) {
    			displayError(e.description);
    		}
    	}
    }
    
    /*=== Buttons ===*/
    function requestButtons() {
    	var ran_number = Math.random()*10;
    	var url = 'get_buttons.php?sid='+chat_id+'&my='+my+'&type='+u_type+'&trash='+ran_number;
    
    	// only continue if xmlHttpGetMessages isn't void
    	if(xmlHttpGetButtons) {
    		try {
    			// don't start another server operation if such an operation
    			//   is already in progress
    			if (xmlHttpGetButtons.readyState == 4 || xmlHttpGetButtons.readyState == 0) {
    				// call the server page to execute the server-side operation
    				xmlHttpGetButtons.open("GET", url, true);
    				xmlHttpGetButtons.onreadystatechange = handleReceivingButtons;
    				xmlHttpGetButtons.send(null);
    			}
    		} catch(e) {
    			displayError(e.description);
    		}
    	}
    }
    
    function handleReceivingButtons() {
    	// continue if the process is completed
    	if (xmlHttpGetButtons.readyState == 4) {
    		// continue only if HTTP status is "OK"
    		if (xmlHttpGetButtons.status == 200) {
    			try {
    				// process the server's response
    				readButtons();
    			} catch(e) {
    				// display the error message
    				displayError(e.description);
    			}
    		} else {
    			// display the error message
    			displayError(xmlHttpGetButtons.statusText);
    		}
    	}
    }
    
    function readButtons() {
    	// retrieve the server's response
    	var response = xmlHttpGetButtons.responseText;
    
    	if (response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 )  displayError('Sever Error '+response);
    	if (response.length == 0) {
    		return;
    	}
    
    	buttons.innerHTML = response;
    }
    
    /*=== Other Functions ===*/
    
    // function that displays an error message
    function displayError(message) {
    	alert("Error accessing the server! "+ "\r\n" + message);
    }
    
    function get_last_chat(Request) {
    	var ret = new Array(2);
    	loc = Request.indexOf('<last_chat>');
    	loc2 = Request.indexOf('</last_chat>');
    	if ((loc-1) <= 0) {
    		ret[0] = '';
    	} else {
    		ret[0] = Request.substring(0, loc-1);
    	}
    	ret[1] = Request.substring(loc+11, loc2);
    	return ret;
    }
    
    function keydown(e) {
    	var characterCode; //literal character code will be stored in this variable
    	e = (!e) ? window.event : e;
    
    	characterCode = (e.charCode) ? e.charCode :
    		 ((e.keyCode) ? e.keyCode :
    		 ((e.which) ? e.which : 0));
    
    	//If Shift
    	if (characterCode == 16) {
    		isDown = true;
    	}
    }
    
    function checkEnter(e){ //e is event object passed from function invocation
    	var characterCode; //literal character code will be stored in this variable
    	e = (!e) ? window.event : e;
    
    	characterCode = (e.charCode) ? e.charCode :
    		 ((e.keyCode) ? e.keyCode :
    		 ((e.which) ? e.which : 0));
    
    	if(characterCode == 13 && (isDown == null || !isDown)){ //if generated character code is equal to ascii 13 (if enter key)
    		send_chat_text(); //submit the form
    		return false;
    	} else {
    		isDown = false;
    		return true;
    	}
    }
    
    function change_chat(chatid) {
    	clearTimeout(timer_chats);
    	//Add the chat just sent
    	chat_content.innerHTML = '';
    	//Set focus back to it
    	chat_text.focus();
    	chat_id = chatid;
    	last_chat = 0;
    	requestButtons();
    	requestChats();
    }
    
    function popitup(url) {
    	window.open(url, "", "menubar=1,resizable=1,width=770,height=380");
    	return false;
    }
    
    function popitupIE(url) {
    	window.open(url);
    	return false;
    }
    
    function loader() {
    	if (detach) window.detachEvent("onload", loader); //just to be safe
    	users_content = xGetElementById("users");
    	chat_text = xGetElementById("chat_text");
    	chat_content = xGetElementById("chat_content");
    	buttons = xGetElementById("chat_buttons");
    	requestUsers();
    	requestChats();
    	chat_text.focus();
    }
    
    
    function unloader() {
    	if (timer_chats) {
    		clearTimeout(timer_chats);
    		timer_chats = null;
    	}
    	if (timer_users) {
    		clearTimeout(timer_users);
    		timer_users = null;
    	}
    	xmlHttpGetUsers = null;
    	xmlHttpGetChats = null;
    	xmlHttpGetButtons = null;
    	xmlHttpGetSendMessage = null;
    	if (detach) window.detachEvent("onunload", unloader); //just to be safe
    }
    
    if (window.addEventListener) {
    	window.addEventListener("load", loader, false);
    	window.addEventListener("unload", unloader, false);
    } else if (window.attachEvent) {
    	detach = true;
    	window.attachEvent("onload", loader);
    	window.attachEvent("onunload", unloader);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured