"Simon Wigzell" <si**********@shaw.ca> writes:
I cobbled together the following function from examples on the internet to
set named spanned items in the parent form.
The worst part of the internet is that there is no quality control :)
It works fine for IE but not at all for netscape. What other browser
conditions do I need to test for and what is the correct syntax for
the sode so that I am covered for all cases?
All cases would include Javascript not being available, or that
changing the document content isn't possible at all. I guess you are
excluding these cases.
function WriteContent(name, newText)
{
var browser=navigator.appName
var version=parseInt(navigator.appVersion)
If you want to use a property or object, test for it specifically
instead of trying to guess the browser version.
if (browser=="Microsoft Internet Explorer")
eval("self.opener.document.all."+name+".innerHTML= '"+newText+"'");
You should never use eval for accessing properties or variables. You
will probably never need to use eval at all. The exceptions are few
and rare, and it is a safe bet that you won't hit them.
This is (almost) equivalent and *much* more effective:
self.opener.document.all[name].innerHTML=newText;
The "almost" refers to the case where the value of newText contains a
single quote ('). In that case, the eval version fails.
else
{
self.opener.document.layers[name].document.close();
document.layers only exist in Netscape 4. Netscape 6+ is a complete
rewrite (it is a branded version of the Mozilla browser) and is not
compatible with Netscape 4.
self.opener.document.layers[name].document.write(newText);
self.opener.document.layers[name].document.close();
}
}
If newText contains a text string, and not HTML, then use the following:
---
function getElem(doc,id) {
if (doc.getElementById) {
return doc.getElementById(id);
} else if (doc.all) {
return doc.all[id];
} else if (doc.layers) {
return doc.layers[id];
}
return null; // this is not happening
}
function writeContent(name,newText) {
var doc = opener.document;
var elem = getElem(doc,name);
if (doc.createTextNode) { // Modern browsers
var text = doc.createTextNode(newText);
while(elem.hasChildNodes()) {
elem.removeChild(elem.lastChild);
}
elem.appendChild(text);
} else if (elem.innerHTML) { // IE 4
elem.innerHTML = newText; // Maybe use .innerText
} else if (elem.document != doc) { // NS 4
elem.document.open();
elem.document.write(newText);
elem.document.close();
}
}
---
No reference to browser name or version.
If newText contains HTML, you will have to drop the first method,
or parse the HTML yourself.
Tested in Netscape 4, Mozilla FB 0.7, IE 6, and Opera 7
/L
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'