Forum Moderators: open

Message Too Old, No Replies

document.write()

Writes weird

         

treeleaf20

7:03 pm on Jun 30, 2004 (gmt 0)

10+ Year Member



Ok I have the following block of code:
<script language="JavaScript" type="text/javascript">
document.write('test')
</script>

and this writes test on the current page where I want it to go. My problem is that when I try and call a function and write it to this same spot it writes it to another page instead of writing it to where the code is placed into the body of the document. The function is this:

function getBC(div_name)
{
document.write(div_name);
}

It gets its argument from this function call:
getBC('Retirement');

I can't figure out why it would write it to a different page instead of where I want it to go in the body of the document like just writing document.write('test') like it was without the function. Anyone have any ideas?

treeleaf20

7:26 pm on Jun 30, 2004 (gmt 0)

10+ Year Member



I think the problem is that since since it doesn't get written to the page when it loads it thinks it needs to load a new page to write it. Does anyone know if you can make it write to the same page even after the whole document was loaded and you call a function with an onClick command?

DrDoc

7:28 pm on Jun 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can use innerText or innerHTML to modify existing content...

treeleaf20

7:30 pm on Jun 30, 2004 (gmt 0)

10+ Year Member



Can you give me an example....not really sure what that is?

treeleaf20

7:35 pm on Jun 30, 2004 (gmt 0)

10+ Year Member



Using my above example is it possible to call the javascript function and put the innerHTML in that same function to have it write to the screen?

treeleaf20

8:01 pm on Jun 30, 2004 (gmt 0)

10+ Year Member



I figured that out. Here is the code I use:

<span ID="bc">Health</span>

<SCRIPT LANGUAGE="JavaScript">
<!--
function getBC(div_name) {
document.getElementById("bc").innerHTML = div_name;
}

// -->
</SCRIPT>

Now I would like to make the div_name into a link. This is what I tried but it is giving me an error:

<span ID="bc">Health</span>

<SCRIPT LANGUAGE="JavaScript">
<!--
function getBC(div_name) {
document.getElementById("bc").innerHTML = '<a href = http:\/\/www.yahoo.com/>'.div_name.'</a>';
}

// -->
</SCRIPT>

Any more ideas?

DrDoc

9:08 pm on Jun 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The JavaScript contactenator is '+', not '.' :)