Click here to Skip to main content
15,033,917 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Okay, so I know that if you have something like the following:

HTML
<html>
<body>
<script type="text/javascript">
document.write("red");
</script>
<p>blue</p>
</body>
</html>


You will obviously get an output that reads:

red
blue


Yet, if you were to write the following:

HTML
<html>
<body>
<script type="text/javascript">
function printred(){document.write("red");}
</script>
blue
<script type="text/javascript">
printred();
</script>
</body>
</html>


Would the output read

red
blue

or
blue
red
?

So I guess my question is, when you add a document.write() to a function, does the javascript write to the line where the function is or where the function is being called? If I wanted to position where my document.write() outputs to (say I want it printed to line 8 instead of my current line, 14).. is that possible? Thanks in advance guys!
Posted
Comments
enhzflep 1-Jul-13 16:56pm
   
I realize this is an obvious question.. however, why don't you try it yourself?
Also, document.write is not such a good function. You're much better adding new elements and placing the text inside them.
i.e
var myPara = document.createElement('p');
myPara.appendChild( document.createTextNode('blue') );
document.body.appendChild(myPara);

myPara = document.createElement('p');
myPara.appendChild( document.createTextNode('red') );
document.body.appendChild(myPara);
Thanks7872 2-Jul-13 1:25am
   
You wasted your time writting this code here,better you wrote it on your screen. You could get better solution there than here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900