function: cannot pass a string and use as an object with netscape!

G

Geniium

Hello,

Im looking to make work my script on both IE and Netscape. It works
fine in IE, but netscape cant handle "dynamic" variables. I need some
help!

Is there a CORRECT way to pass a string as parameter and then use it
as an object in Netscape? IE does that without problem... and Netscape
seems not able to handle it...

1. Here is the function :

function change_text(object, new_value){
if (document.all) { // IE
object.innerHTML = new_value;
} else { // Netscape => this DONT work
object = document.getElementById(object);
object.innerHTML = new_value;
}
}

2. and an exemple of script calling it :

change_text(divNameToChange, 'new html content);

I think u might see my problem... spent few hours looking here and
there, mainly on google... without success. tried many different
things to make the script work.
A workaround is to pass the full object (like
document.getElementById('divNameToChange')) to handle it on netscape,
but I have tonz of line of code using it, I dont want to change
manually all and its not a pretty way to do it.

If you got any idees, shoot! :)

Thanks!

Romain

PS: eval() seems not a good way to do it, and is not working.
 
M

Martin Honnen

Geniium said:
Im looking to make work my script on both IE and Netscape. It works
fine in IE, but netscape cant handle "dynamic" variables. I need some
help!

Is there a CORRECT way to pass a string as parameter and then use it
as an object in Netscape? IE does that without problem... and Netscape
seems not able to handle it...

1. Here is the function :

function change_text(object, new_value){
if (document.all) { // IE
object.innerHTML = new_value;
} else { // Netscape => this DONT work
object = document.getElementById(object);
object.innerHTML = new_value;
}
}

2. and an exemple of script calling it :

change_text(divNameToChange, 'new html content);

If you have
<div id="divId"
then you need to call
change_text('divId', '<p>Kibology<\/p>');
and use
function change_text(elementId, html) {
if (document.all) {
document.all[elementId].innerHTML = html;
}
else if (document.getElementById) {
document.getElementById(elementId).innerHTML = html;
}
}

That will work without problems with Netscape 6/7, IE4+, Opera 7.
Netscape 4 doesn't allow you to change the innerHTML of an element,
unless it is positioned absolutely where you then need to document.write
the new content.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top