0

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?

3
Contributors
7
Replies
33
Views
7 Years
Discussion Span
Last Post by priakant
2

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

Edited by Airshow: n/a

0

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>?
0

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

0

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

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.