Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ajax script 1

Status
Not open for further replies.

kyern

Programmer
Mar 30, 2006
36
US
I am trying to create a ajax script that checks for updates to a database. right now it runs whiteboard_total.asp which returns a number if that number is larger than the stored var it will update the contents. What I have doesn't update the content, it isn't giving an error. I am very new to ajax so hoping for some insight here. The script:

Code:
var whiteboard_total = 0;
		
		function makeRequest(url) {
	        var http_request = false;

	        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
	            http_request = new XMLHttpRequest();
	            if (http_request.overrideMimeType) {
	                http_request.overrideMimeType('text/xml');
	            }
	        } else if (window.ActiveXObject) { // IE
	            try {
	                http_request = new ActiveXObject("Msxml2.XMLHTTP");
	            } catch (e) {
	                try {
	                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
	                } catch (e) {}
	            }
	        }

	        if (!http_request) {
	            alert('Giving up :( Cannot create an XMLHTTP instance');
	            return false;
	        }
	        http_request.onreadystatechange = function() { checkTotal(http_request); };
	        http_request.open('GET', url, true);
	        http_request.send(null);
			
			setTimeout("makeRequest('whiteboard_total.asp?division=nc')", 5000); 
	    }

		function checkTotal(http_request) {
	        if (http_request.readyState == 4) {
	            if (http_request.status == 200) {
					if (whiteboard_total >= 0) {
						temp_total = parseInt(http_request.responseText);
						if (temp_total > whiteboard_total) {
							whiteboard_total = temp_total;
							getContent('whiteboard_output.asp?division=nc');
							self.focus();
						}else{
							whiteboard_total = temp_total;
						}
					}
	                //alert(http_request.responseText);
	            } else {
	                alert('There was a problem with the request.');
	            }
	        }
	    }
		
		function getContent(url) {
	        var http_request = false;

	        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
	            http_request = new XMLHttpRequest();
	            if (http_request.overrideMimeType) {
	                http_request.overrideMimeType('text/xml');
	            }
	        } else if (window.ActiveXObject) { // IE
	            try {
	                http_request = new ActiveXObject("Msxml2.XMLHTTP");
	            } catch (e) {
	                try {
	                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
	                } catch (e) {}
	            }
	        }

	        if (!http_request) {
	            alert('Giving up :( Cannot create an XMLHTTP instance');
	            return false;
	        }
	        http_request.onreadystatechange = function() { updateContents(http_request); };
	        http_request.open('GET', url, true);
	        http_request.send(null);
	    }
		
		function updateContents(http_request) {
	        if (http_request.readyState == 4) {
	            if (http_request.status == 200) {
					document.getElementById('content').value = http_request.responseText
	            } else {
	                alert('There was a problem with the request.');
	            }
	        }
	    }
		
		window.onload=makeRequest('whiteboard_total.asp?division=nc')
 
I've made some changes and now I finally get the content, now I have another problem. When the script checks whiteboard_total.asp if there are new entries its supposed to update, I can add new entries all day and it only pulls content when the page loads.

Code:
var whiteboard_total = 0;
		
		function makeRequest(url) {
	        var http_request = false;

	        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
	            http_request = new XMLHttpRequest();
	            if (http_request.overrideMimeType) {
	                http_request.overrideMimeType('text/xml');
	            }
	        } else if (window.ActiveXObject) { // IE
	            try {
	                http_request = new ActiveXObject("Msxml2.XMLHTTP");
	            } catch (e) {
	                try {
	                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
	                } catch (e) {}
	            }
	        }

	        if (!http_request) {
	            alert('Giving up :( Cannot create an XMLHTTP instance');
	            return false;
	        }
	        http_request.onreadystatechange = function() { checkTotal(http_request); };
	        http_request.open('GET', url, true);
	        http_request.send(null);
	    }
		
		function checkTotal(http_request) {
	        if (http_request.readyState == 4) {
	            if (http_request.status == 200) {
					if (whiteboard_total >= 0) {
						temp_total = parseInt(http_request.responseText);
						if (temp_total > whiteboard_total) {
							whiteboard_total = temp_total;
							getContent('whiteboard_output.asp?division=nc');
							alert(temp_total);
						}else{
							whiteboard_total = temp_total;
						}
					}
					setTimeout("makeRequest('whiteboard_total.asp?division=nc')", 50);
	            } else {
	                alert('There was a problem with the request.');
	            }
	        }
	    }
		
		function getContent(url) {
	        var http_request = false;

	        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
	            http_request = new XMLHttpRequest();
	            if (http_request.overrideMimeType) {
	                http_request.overrideMimeType('text/xml');
	            }
	        } else if (window.ActiveXObject) { // IE
	            try {
	                http_request = new ActiveXObject("Msxml2.XMLHTTP");
	            } catch (e) {
	                try {
	                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
	                } catch (e) {}
	            }
	        }

	        if (!http_request) {
	            alert('Giving up :( Cannot create an XMLHTTP instance');
	            return false;
	        }
	        http_request.onreadystatechange = function() { updateContents(http_request); };
	        http_request.open('GET', url, true);
	        http_request.send(null);
	    }
		
		function updateContents(http_request) {
	        if (http_request.readyState == 4) {
	            if (http_request.status == 200) {
					document.getElementById("content").innerHTML = http_request.responseText;
					self.focus();
	            } else {
	                alert('There was a problem with the request.');
	            }
	        }
	    }
		window.onload = function() { makeRequest('whiteboard_total.asp?division=nc'); };
 
Its hard to see what the problem could be, but i suggest you get this exstension for firefox it will help you greatly with js and ajax you can see what the error is specificly


Also in the drop downs to the left when you open it, under options you have to check "Show XMLHttpRequests" this should help you solve your issue, if not post back her again and either i can help or some other skilled javascript person can that is here...

Army : Combat Engineer : 21B
 
Ok, I think I'm going to go hit my head somewhere. It works perfect in firefox. It turns out that the setTimeout function is also working in IE. The difference is that in firefox, if a entry is added it actually puts new content in the div, in IE it isn't updating the page.

Thanks for the suggestion on the debugger, that is fantastic. I just wish i could figure out why it doesn't work in ie. I suppose I could refresh the page but I needed to get away from that. Any insight would be wonderful.

David
 
Try this:

Code:
var random_num = (Math.round((Math.random()*1000)+1))

setTimeout("makeRequest('whiteboard_total.asp?division=nc[highlight]&random=' + random_num[/highlight])", 50);

I had this happen just the other day... but it was FF that wasnt reloading and IE was working... i hate when that happens... if you do this... it tricks the browser into thinking its going to be new content... its worth a shot...


Jason

Army : Combat Engineer : 21B

 
Fantastic! That worked perfect. Thank You!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top