I was posting code to Chrome successfully and then it suddenly stopped working, but I don't know why. It shows the title at the tab (which is Demo) but shows a blank white page.
Here is my code. I open the file (after savings it) in Chrome.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Demo</title>
<script type = "text/javascript">
<!--
var place = "Delta";
var type = "blues";
function music {
place = "New Orleans";
type = "jazz"
document.write("I like the " + place + " " + type);
}
//-->
</script>
</head>
<body>
<script type = "text/javascript">
<!--
music();
document.write(" but I really like " + place + " " type);
//-->
</script>
You have two errors in your bit of code, you have missed out the brackets when you declared your function music.
The line:
Code:
function music{
Should be:
Code:
function music(){
and you have missed out a plus sign in your document write statement, the line:
Code:
document.write("I like the " + place + " " type);
Should be:
Code:
document.write("I like the " + place + " " + type);
Just as an aside you should try and find a different way of writing to the screen rather than using document.write() as it is no longer considered a good coding practice
...
Just as an aside you should try and find a different way of writing to the screen rather than using document.write() as it is no longer considered a good coding practice
And, if executed after the page has been rendered, it will cause the browser to RELOAD your page as if you had never been there before.
Not good for all practical purposes.
Better to use .innerHTML on a <div> or other tag element.
thanks so much for the tip! that's too bad that the instructions tell you to use it. Codecademy uses console.log to print, do you think this one is okay?
actually, I just used .innerHTML (instead of document.write) and also tried console.log, and neither one of those work with this script. I noticed that W3Schools uses document.write, still.
thanks Vincent, so that is how you use it. i have not learned how to use the string of code that uses innerHTML yet, but i will keep this in mind as i continue to learn. it's nice to know there are many options to achieve something. thanks so much for your input.
Bookmarks