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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

try different function calls until one of them works

Status
Not open for further replies.

Tracey

Programmer
Oct 16, 2000
690
NZ
Hi
I'm using prototype.js AJAX library to add AJAX to my app.

In the documentation, reference is made to the fact that xmlNode.text works in some browsers, and xmlNode.textContent works in other browsers. They suggest that to get around this problem i should use the following function:

Code:
<script>
function getXmlNodeValue(xmlNode){
	return Try.these(
		function() {return xmlNode.text;},
		function() {return xmlNode.textContent;}
		);
}
</script>

Which is great because IE works using .text and firefox wants textContent. (using .text in firefox returns "undefined" and using .textcontent in IE returns "undefined".

However, the above function returns "undefined" in both browsers. could someone here help me fix this? Pretty Please?

Tracey
Remember... True happiness is not getting what you want...

Its wanting what you have got!
 
You're probably passing in bananas to the function. Or bathtubs. Or perhaps jam in a sock?

Seriously... (no car / garage analogies here this time)... how on earth would be be able to know why it's not working without knowing how you're calling it, and with what data (the XML in question would help - or a URL to the page).

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Phew.. thanks Dan

A fresh start on monday [morning] and a good healthy dose of peer sarcasm [poke] did the job [smile2]

[cheers]

Tracey

Tracey
Remember... True happiness is not getting what you want...

Its wanting what you have got!
 
ahhh spoke too soon

working in IE but not in firefox:

Code:
function testChoice(user){
		    $('progress').innerHTML = 'Processing request';
			var req = new Ajax.Request('test.xml',{method:'get',onComplete:processResult});
		}
		function processResult(response){
			var val = response.responseXML;
			//debugger;
			var testObj = val.getElementsByTagName('name');
			var addrObj = val.getElementsByTagName('address');
			
			$('name1').value = getXmlNodeValue(testObj.item(0));
			$('address').value = getXmlNodeValue(addrObj.item(0));
			$('progress').innerHTML = '';
		}

	function getXmlNodeValue(xmlNode){
		return Try.these(
			function() {return xmlNode.text;},
			function() {return xmlNode.textContent;}
			);
	}

Tracey
Remember... True happiness is not getting what you want...

Its wanting what you have got!
 
If you're after the text value contained in a node, try using this... I thought nodeValue was the standard way of doing this - but I could be wrong:

Code:
function function getXmlNodeValue(xmlNode) {
   return xmlNode.nodeValue;
}

However, before doing this, mke sure what you're passing into the "getXmlNodeValue" function ("xxx.item(0)" by the looks of it) is defined - us alerts / console (FF with Firebug) / etc.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi Dan

Thanks for replying again.

I get "null" (in IE) and blank nothingness (in Firefox) instead of the value of the node when i use nodeValue.

Yes the value is set and is there. If i revert back to .text i see the value of the node in my input, but still "undefined" in firefox.

Now, i understand i need to use .text for IE and .textContent for firefox/mozilla. So the function getXmlNodeValue as it stands works for IE but for some reason does not return textValue for Firefox.



Tracey
Remember... True happiness is not getting what you want...

Its wanting what you have got!
 
See if this works for FF:
Code:
		var range = document.createRange();
		range.selectNodeContents(xmlNode);
		var text = range.toString()

Let us know your results!

x
 
Hi Xaqte.

That works for Firefox fine, but errors in IE.
i tried using it instead of the second function in getXmlNodeValue() but it still causes errors in IE:

Code:
function getXmlNodeValue(xmlNode){
	return Try.these(
		function() {return xmlNode.text;},
		//function() {return xmlNode.textContent;}
		function() {return getFoxNodeValue(xmlNode);}
		);
}

function getFoxNodeValue(xmlNode){
		var range = document.createRange();
       		range.selectNodeContents(xmlNode);
        	var text = range.toString();
		return text;

}

The ultimate goal here is to get the original function working, as i know .text works for IE and i know .textContent works in Firefox. What does everyone else do when faced with using these properties?


Tracey
Remember... True happiness is not getting what you want...

Its wanting what you have got!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top