PDA

View Full Version : dynamic string



chrismiceli
Nov 27th, 2002, 02:58 AM
how can i update a string written at the bottom of a page, like this


<script type="text/javascript">
name = "hi";
</script>
//html code
<script type="text/javascript">
document.write(name)
</script>

if i changed "name", how could I get it to be changed at the end of the page?

A1ien51
Nov 27th, 2002, 04:08 AM
you would have to do it with innerHTML.

Basic idea

<html>
<head>
<script>
function DisplayText(wlayer,OutText){
if (document.getElementById) {
document.getElementById(wlayer).innerHTML = OutText;
}
else if (document.all) {
document.all[wlayer].innerHTML = OutText;
}
else if (document.layers) {
document.layers[wlayer].innerHTML = OutText;
}
}


function HelloThere(){
Q1=prompt("What is Your Name","");
DisplayText("A1ien51",Q1);
Q2=prompt("What is Red","");
DisplayText("UFO",Q2);
}

window.onload=HelloThere;
</script>
<title>A1ien51 using innerHTML</title>
</head>

<body>
<div id="A1ien51">Hello There</div>
<span id="UFO">flames</span>
</body>
</html>

chrismiceli
Nov 27th, 2002, 04:28 AM
do you need the if commands for cross browser, or can i just do the getElementsById for all browsers?

glenngv
Nov 27th, 2002, 05:18 AM
yes you need that.
and for it to truly work with ns4, the code should be like this:

else if (document.layers) {
document.layers[wlayer].document.write(OutText);
document.layers[wlayer].document.close();
}

and the span tag should be absolutely positioned for it to be considered a layer in NS4:

<span id="UFO" style="position:absolute">flames</span>

chrismiceli
Nov 27th, 2002, 05:22 AM
this makes me ask myself this

http://codingforums.com/showthread.php?threadid=10318

glenngv
Nov 27th, 2002, 05:31 AM
I agree with that too. I just corrected A1ien51's code for NS4.

If you really don't want to code for NS4, you could do this:

function DisplayText(wlayer,OutText){
if (document.getElementById) {
document.getElementById(wlayer).innerHTML = OutText;
}
else if (document.all) {
document.all[wlayer].innerHTML = OutText;
}
else if (document.layers) {
alert("Please use modern browser to visit this site!")
}
}

chrismiceli
Nov 27th, 2002, 05:37 AM
i like that glen, i will use it.:D