How to display text string with Javascript on screen?

This is my test page:

<html>
test
<script type="text/javascript">
	  docwrite(1);
          document.write('hi');
	  docwrite('hello');
</script>
docwrite(2);

<script type="text/javascript">
function docwrite(x)
{
  document.write(x);
}
</script>
</html>

I saw only "test docwrite(2);".
I want to see "test 1 hi hello docwrite(2);" with " 1 hi hello" coming from Javascript.
How to do that?

Recommended Answers

All 7 Replies

Try this,

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Untitled</title>
<script>
function docwrite(x)
{
  document.write(x);
}
</script>
</head>

<body>

test<br />

<script type="text/javascript">
docwrite(1);
document.write(' hi');
docwrite(' hello');
docwrite('<br />');
</script>

docwrite(2);

</body>
</html>

Note how docwrite is defined in the <head> and it is called from the <body> .

Note also that "docwrite(2);" is outside the <script></script> tags, and is therefore treated as regular text, not Javascript.

Airshow

Thanks!

Try this,

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Untitled</title>
<script>
function docwrite(x)
{
  document.write(x);
}
</script>
</head>

<body>

test<br />

<script type="text/javascript">
docwrite(1);
document.write(' hi');
docwrite(' hello');
docwrite('<br />');
</script>

docwrite(2);

</body>
</html>

Note how docwrite is defined in the <head> and it is called from the <body> .

Note also that "docwrite(2);" is outside the <script></script> tags, and is therefore treated as regular text, not Javascript.

Airshow

Is there a shorthand for:

<script type="text/javascript">
   docwrite('hi');
</script>?

You could put it all on one line

<script type="text/javascript">docwrite('hi');</script>

Or you could just write plain Hi or with some markup <p>Hi</p> .

Writing with javascript, docwrite('Hi') about as simple as it gets.

Airshow

Is there any shorthand for the construct:
<script type="text/javascript">
</script>

You can simplify to <script>...</script> .

To my best knowledge type="text/javascript is assumed by all the major browsers, formally so as of HTML5.

There's no equivalent of <% ... %> as in php/jsp. Reason being Javascript runs in the browser, which needs well formed tags in order to distinguish the code from text to be displayed.

Airshow

i want this function of text area plz help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.