-3

I am new to javascript and trying to execute the following code, could anyone tell me why only first document.write is being executed not the other ones.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>my first java script</title>
</head>
<body>
<script type="text/javascript">
var myhello="hello world, welcome to java script";
var heading="a page of java script";
var linktag="<a href=\"http://www.google.com\">wanna search on google</a>";
var redtext="<span style=\"color:red\">I am so colorful today!</span>";
var begineffect="<strong>";
var endeffect="</strong>";
var beginpara="<p>";
var endpara="</p>";
document.write(begineffect+heading+endeffect);
document.write(begingpara);
document.write(hello);
document.write(endpara);
document.write(begingpara);
document.write(linktag);
document.write(endpara);
document.write(beginpara);
document.write(redtext);
document.write(endpara);
</script>
</body>
</html>

I have tested the following code in all web browser.

Ben Rowe
  • 26,794
  • 6
  • 50
  • 72
Mike
  • 1,819
  • 5
  • 26
  • 31
  • 3
    You said you were new to javascript, so I'll give you a heads up. Don't use document.write(). Read more about why here http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – timrwood Oct 04 '11 at 00:33

3 Answers3

5

It is generating an error because you don't have a variable called hello

var hello = 'define something here';
document.write(hello);

Using a good browser like chrome, or firefox+firebug will reveal errors like this if you use the web inspector.

http://www.google.com/chrome/intl/en/webmasters-faq.html#jsexec

Ben Rowe
  • 26,794
  • 6
  • 50
  • 72
4

If you check your console (F12 in Chrome, or load Firebug for Firefox) you see this error:

Uncaught ReferenceError: begingpara is not defined

You have many typos and incorrect variable names (i.e. you have defined variables but used a different name when referencing them) - correct them and your code will run.

Andy
  • 16,265
  • 9
  • 48
  • 69
-1

It overwrites everything so the others no longer exist

yoozer8
  • 6,966
  • 6
  • 47
  • 84