r/javascript • • Aug 16 '11

Weird result in IE

I have created a page which contains multiple languages. All the text is stored in a double array. text[language][text_id]. Example:

text[0][0] = "Allo"; text[0][1] = "Comment ça va?"; text[1][0] = "Hi"; text[1][1] = "How are you?";

I insert the text in my elements that contains the class "ts". I specify the text id in a attribute I created call "t". Example:

<p class="ts" t="0"></p> would write "Hi" if the language is set to 1.

To achieve my goal I decided to use document.getElementsByClassName("ts") and loop through all the elements but obviously IE does not support it. So I created my own function to recreate getElementsByClassName in IE. From that point, everything works fine in Opera, Firefox and Chrome but Internet Explorer has a weird behaviour. At first ie skips the first element so I created another one on top to test the result. It changed the top div to undefined. This is the code.

<html> <head> <title>ContacMe</title> <script> texte = new Array(); texte[0] = new Array(); texte[0][0] = "Rechercher"; texte[0][1] = "Ou..."; texte[0][2] = "Créer un profil"; texte[1] = new Array(); texte[1][0] = "Find"; texte[1][1] = "Or..."; texte[1][2] = "Create a profil"; langue = 0;

		function gEBCN_ie(classe) {
			el_body = document.body.childNodes;
			ts = new Array();
			for (var i = 1; i < el_body.length; i++) {
				if (el_body[i].childNodes.length >= 1) {
					sous_elements(el_body[i]);
				}
				ts.push(el_body[i]);
			}
			return ts;
		}
		function sous_elements(el) {
			for (var i = 1	; i < el.length; i++) {
				if (el[i].childNodes.length >= 1) {
					sous_elements(el[i]);
				}
				if (el[i].getAttribute("class") == "ts")
					ts.push(el[i]);
			}
		}
		function init() {
			if (document.getElementsByClassName)
				ts = document.getElementsByClassName("ts");
			else
				ts = gEBCN_ie("ts");
			for (var i = 0; i < ts.length; i++) {
					ts[i].innerHTML = texte[langue][parseInt(ts[i].getAttribute("t"))];
			}
		}
	</script>
</head>
<body onload="init()">
	<p class="ts" t="0"></p>
	<div><input type="text" /><button class="ts" t="0"></button></div>
	<p class="ts" t="1"></p>
	<button class="ts" t="2"></button>
</body>

</html>

Can anybody save me from this ie nightmare please?

EDIT:: This is my final code..

<html> <head> <title>ContacMe</title> <script> var texte = [ [ "Rechercher", "Ou...", "Créer un profil" ], [ "Find", "Or...", "Create a profile" ] ]; langue = 0;

		function gEBCN_ie(classe) {
			el = document.body.getElementsByTagName("*");
			ts = new Array();
			for (var i = 0; i < el.length; i++) {
				reg = new RegExp(classe);
				if (el[i].className.match(reg))
					ts.push(el[i]);
			}
			return ts;
		}

		function init() {
			if (document.getElementsByClassName)
				ts = document.getElementsByClassName("ts");
			else
				ts = gEBCN_ie("ts");
			for (var i = 0; i < ts.length; i++) {
					ts[i].innerHTML = texte[langue][parseInt(ts[i].getAttribute("t"))];
			}
		}
	</script>
</head>
<body onload="init()">
	<div><input type="text" /><button class="ts" t="0"></button></div>
	<p class="ts" t="1"></p>
	<button id="bt_2" class="ts" t="2"></button>
</body>

</html>

0 Upvotes

36 comments sorted by

View all comments

Show parent comments

2

u/StoneCypher Aug 18 '11

I just threw up a little in my mouth. This, in a comment chain where someone else claimed using libraries like Prototype makes code more maintainable.

Thank you for explaining that. I now want to punch ECMA-262 in the face. Please enjoy an upboat on me, and if you find yourself in the Washington DC area, a beer or two as well.