JavaScript Tutorial/Document/write

Материал из Web эксперт
Перейти к: навигация, поиск

Do calculation in document.write

<html>
<head>
<title>Find the area and circumference of a circle</title>
<script type="text/javascript" language="javascript">
<!-- //
function FindArea(){
    var number = new Number(123);
    document.write("<P>You entered a radius of " + number + "</p>");
    document.write("<P>The area of the circle is " + (number * number * Math.PI) + "</p>");
    document.write("<P>The circumference of the circle is " + (2 * number * Math.PI) + "</p>");
}
// -->
</script>
</head>
<body onload="FindArea()">
</body>
</html>


document.writeln()

Syntax



document.writeln(value,....)


Escape quotation marks in document.write

<HTML>
<BODY>
<SCRIPT language="JavaScript">
<!--
    document.write("John said, \"This project is fun!\"");
//-->
</SCRIPT>
</BODY>
</HTML>


Output all array element with document.write

<html>
<head>
<title>Writing an Array</title>
</head>
<body>
<P>
<script language="javascript" type="text/javascript">
<!--
var myArray = new Array();
myArray[0] = "AAA";
myArray[1] = "BBB";
myArray[2] = "CCC";
for (var i=0; i<myArray.length; i++) {
  document.write("Element " +i+ " contains: " +myArray[i]+ "<br />");
}
//-->
</script>
</p>
</body>
</html>


Output the HTML tags by using the document.write

<HTML>
<BODY>
<SCRIPT language="JavaScript">
<!--
    var myheading="This is My Web Page!";
    var linktag="<A HREF="http://www.wbex.ru">Web Site Link!</A>";
    var sometext="This text can be affected by other statements.";
    var begineffect="<U>";
    var endeffect="</U>";
    var newsection="<BR>";
    document.write(myheading);
    document.write(newsection);
    document.write(begineffect);
    document.write(sometext);
    document.write(endeffect);
    document.write(newsection);
    document.write(linktag);
    document.write(newsection);
    document.write(sometext);
//-->
</SCRIPT>
</BODY>
</HTML>


Use document.write between the HTML tags

<HTML>
<BODY>
<B>start</B>
<P>
<SCRIPT language="JavaScript">
<!--
    document.write("This came from my script, and is now on the page!");
//-->
</SCRIPT>
<P>
<B>end</B>
</BODY>
</HTML>


Use document.write() to output the HTML tags

<html>
<head>
<title>Writing HTML</title>
</head>
<body>
<script language="javascript" type="text/javascript">
<!--
document.write("<h1>Writing HTML</h1>");
var myPara  = "<P> para </p> ";
    myPara += "<P><B>document object"s write() method!</B></p>";
window.document.write(myPara);
//-->
</script>
</body>
</html>


Writing to a Document

Syntax



document.write(value,....)