You are not logged in.

  • Login

1

Tuesday, November 29th 2011, 12:13am

Ajax und der FF

Hallo,

derzeit versuche ich mich an einem kleinen Ajax Chat für eine Private Seite.
Soweit läuft auch alles im Opera aber im Firefox nicht.

Ich habe 2 Ajax Request

JavaScript Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
function ajaxFunction(){
	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;
			}
		}
 
	}
	return ajaxRequest;
}
 
function showData() {
	//alert("i m here");
	htmlRequest = ajaxFunction();
	if (htmlRequest==null){ // If it cannot create a new Xmlhttp object.
		alert ("Browser does not support HTTP Request"); // Alert Them!
		return; // Returns.
	} // End If.
 
	htmlRequest.onreadystatechange = function(){
		if(htmlRequest.readyState == 4){
			document.getElementById("shoutarea").innerHTML = htmlRequest.responseText;
			var obj = document.getElementById('shoutarea');
			obj.scrollTop = obj.scrollHeight;
 
		}
	}
 
	htmlRequest.open("GET", "index.php?mode=outputinfo", true);
	htmlRequest.send(null);
}
 
showData();
setInterval("showData()",1000);
 
function showsData() {
	//alert("i m here");
	htmlRequest = ajaxFunction();
	if (htmlRequest==null){ // If it cannot create a new Xmlhttp object.
		alert ("Browser does not support HTTP Request"); // Alert Them!
		return; // Returns.
	} // End If.
 
	htmlRequest.onreadystatechange = function(){
		if(htmlRequest.readyState == 4){
			document.getElementById("onlinearea").innerHTML = htmlRequest.responseText;
			var obj = document.getElementById('onlinearea');
			obj.scrollTop = obj.scrollHeight;
 
		}
	}
 
	htmlRequest.open("GET", "index.php?mode=outputinfos", true);
	htmlRequest.send(null);
}
 
showsData();
setInterval("showsData()",1000);


function showData() ruft die Chat Texte Live ab, function showsData() die User Online Liste

Im Opera läuft alles soweit. Die Texte haben zwar ne Verzögerung beim 1 Aufruf von ca 10 Sec aber es läuft.
Im Firefox allerdings habe ich in beiden DIV Spalten die User Online Liste.

So ganz blicke ich auf dem JS Feld noch nicht ganz durch und hoffe auf eure Hilfe.

2

Wednesday, November 30th 2011, 9:22am

HiHo,

So, nach durchstöbern des Forum bin ich auf http://www.easy-coding.de/wiki/php/uploa…t-php-ajax.html gestossen und habe mir diese Funktion dex JS für die User Online Liste zu eigen gemacht.
Nun habe ich 2 unterschiedliche funktionen für den ajax request

Das wäre einmal für die Texte

JavaScript Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// JavaScript Document
 
function ajaxFunction(){
	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;
			}
		}
 
	}
	return ajaxRequest;
}
 
 
function showData() {
	//alert("i m here");
	htmlRequest = ajaxFunction();
	if (htmlRequest==null){ // If it cannot create a new Xmlhttp object.
		alert ("Browser does not support HTTP Request"); // Alert Them!
		return; // Returns.
	} // End If.
 
	htmlRequest.onreadystatechange = function(){
		if(htmlRequest.readyState == 4){
			document.getElementById("shoutarea").innerHTML = htmlRequest.responseText;
			var obj = document.getElementById('shoutarea');
			obj.scrollTop = obj.scrollHeight;
 
		}
	}
 
	htmlRequest.open("GET", "index.php?mode=outputinfo", true);
	htmlRequest.send(null);
}
 
showData();
setInterval("showData()",1000);


Und einmal für die Online Liste

JavaScript Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function showsData() {
		var req;
	 	try {
			req = window.XMLHttpRequest?new XMLHttpRequest(): 
				new ActiveXObject("Microsoft.XMLHTTP"); 
		} catch (e) {
			try{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
 
		req.onreadystatechange = function() {
			if ((req.readyState == 4) && (req.status == 200)) { 
				document.getElementById("onlinearea").innerHTML = req.responseText;
				var obj = document.getElementById('onlinearea');
				obj.scrollTop = obj.scrollHeight;
			}
		}
		req.open("GET", "index.php?mode=outputinfos", true);
		req.send(null);
	}
 
showsData();
setInterval("showsData()",1000);


Bei der Online Liste habe ich also diesmal

JavaScript Code

1
2
req = window.XMLHttpRequest?new XMLHttpRequest(): 
				new ActiveXObject("Microsoft.XMLHTTP");


direkt in die Funktion geschrieben.

Daraus schliesse ich das jede funktion ihren eigenen Ajax Request braucht. Liege ich da richtig?

3

Wednesday, November 30th 2011, 10:15am

Hi,

zum Verständnis.
Ein Request ist eine Anfrage an den Server. Zu dem Request gehört auch eine Response, die man optional verarbeiten kann. Für diesen Ablauf ist eine XML-HTTP Instanz nötig. Werden also 10 Anfrage gesendet, muss das Objekt 10mal instanziert werden.
Die Funktion "ajaxFunction" die du nutzt, instanziert zu jedem Request das Objekt und gibt dieses zurück. Du musst diese Funktionalität also nicht in die anderen Funktionen reinkopieren, sondern kannst die Funktion für alle deine AjaxRequests nutzen.


JavaScript Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function ajaxFunction() {
	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;
			}
		}
 
	}
 
        if (htmlRequest==null){ // If it cannot create a new Xmlhttp object.
		alert ("Browser does not support HTTP Request"); // Alert Them!
		return false; // Returns.
	} // End If.
 
	return ajaxRequest;
}
function showData() {
	//htmlRequest = ajaxFunction();
	var htmlRequest = ajaxFunction();
	htmlRequest.onreadystatechange = function(){
		if(htmlRequest.readyState == 4){
			document.getElementById("shoutarea").innerHTML = htmlRequest.responseText;
			var obj = document.getElementById('shoutarea');
			obj.scrollTop = obj.scrollHeight;
 
		}
	}
 
	htmlRequest.open("GET", "index.php?mode=outputinfo", true);
	htmlRequest.send(null);
}
function showsData() {
       //htmlRequest = ajaxFunction();
       var htmlRequest= ajaxFunction();
       htmlRequest.onreadystatechange = function() {
			if ((htmlRequest.readyState == 4) && (htmlRequest.status == 200)) { 
				document.getElementById("onlinearea").innerHTML = htmlRequest.responseText;
				var obj = document.getElementById('onlinearea');
				obj.scrollTop = obj.scrollHeight;
			}
		}
 
	htmlRequest.open("GET", "index.php?mode=outputinfos", true);
	htmlRequest.send(null);
}
 
showData();
showsData();
setInterval("showData()",1000);
setInterval("showsData()",1000);

4

Wednesday, November 30th 2011, 10:31am

Hallo,

Nein gerade nicht. Wenn ich dies so mache ( Dies habe ich übrigens bereits in meinem 1 Beispiel so gemahct, erster psot) dann geht alles nur mit Extremer verzögerung und im FF habe ich in beiden DIVS die Online Liste

5

Wednesday, November 30th 2011, 2:04pm

Hast du ein Live beispiel was man sich mal ansehen kann?

6

Wednesday, November 30th 2011, 3:53pm

Hallo,

Nein, Dies läuft derzeit local bis die Entwicklung abgeschlossen ist.

7

Sunday, December 4th 2011, 5:50pm

Variablen solltest du mit einem "var" deklarieren. Ansonsten ist die Variable global und es ist kein Wunder, dass es zu Überlappungen kommt.

Beispiel

JavaScript Code

1
2
function showData() {
	var htmlRequest = ...


Du kannst deinen JavaScript Code übrigens mit jslint analysieren lassen: http://www.jslint.com/

Similar threads

Social bookmarks