简体   繁体   中英

JavaScript is Not Working in any of my Browsers

I am just learning JavaScript and for some reason this simple (Example 1 from Chapter One ) example is not working (Jon Duckett's JavaScript and Jquery book) in any of my browsers: FireFox, IE, and Chrome. I made sure JavaScript is turned on. I also put noscript tags in my index.html file to make sure I had JavaScript on. My jsFiddle works perfectly, but nothing in my browsers. Here is my jsFiddle with everything I have in my Atom editor. My links are correct because I copied the full path and then pasted it into the src. I then deleted all the C:\\users....... until I got into the folder with all of the files to end up with what is in the jsFiddle.

html:

<!doctype html>
<html lang="en">
<head>
<meta charset="etf-8" />
<title>Greeting JavaScript</title>
<link rel="stylesheet" href="css/styles.css">
<script src="scripts/greeting.js"></script>
</head>
<body>
<noscript>Your Browser Does Not Support JavaScript.  Please Enable It.</noscript>
<section>
<div id="header">
  <h1>Constructive &amp; Co.</h1>
</div>
<h3 id="greeting"></h3>
<p>For all orders and inquiries please call <em>555-3344</em></p>
</section>
</body>
</html>

javascript:

var today = new Date();
var hourNow = today.getHours();
var greeting;

if (hourNow > 18) {
 greeting = 'Good evening!';
} else if (hourNow > 12) {
 greeting = 'Good afternoon!';
} else if (hourNow > 0) {
 greeting = 'Good morning!';
} else {
 greeting = 'Welcome!';
}

document.getElementById("greeting").innerHTML = greeting;

css:

@import url(http://fonts.googleapis.com/css?family=Open+Sans:800italic);

body {
font-family: "Courier New", Courier, monospace;
/*background: url("../images/constructive-backdrop.jpg") no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;*/
background: #a18957;
margin: 0px;
padding: 0px;
text-align: center;
}
section {
margin-top: 20px;
margin-left: 20px;
height: 500px;
width: 400px;
background: #eee;
border: 1px solid #292929;
}
#header {
height: 200px;
margin: 10px;
background: rgba(227, 192, 186, 0.78);
}
h1 {
margin: 0px;
position: relative;
top: 45%;
}
h3 {
height: 100px;
margin: 10px;
background: red;
}
p {
margin: 10px;
height: 100px;
}

Thank You

You want to add the script tag at the end of your html. The element greeting doesn't exist, when you are executing it.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>Greeting JavaScript</title>
  <link rel="stylesheet" href="css/styles.css">
</head>
<body>
  <noscript>Your Browser Does Not Support JavaScript.  Please Enable It.</noscript>
  <section>
    <div id="header">
      <h1>Constructive &amp; Co.</h1>
    </div>
    <h3 id="greeting"></h3>
    <p>For all orders and inquiries please call <em>555-3344</em></p>
  </section>
  <script src="scripts/greeting.js"></script>
</body>
</html>

Or you leave it in the head and wrap the script into

addEventListener("readystatechange", function () {
  if (document.readyState === "interactive") {

    var today = new Date();
    var hourNow = today.getHours();
    var greeting;

    if (hourNow > 18) {
      greeting = 'Good evening!';
    } else if (hourNow > 12) {
      greeting = 'Good afternoon!';
    } else if (hourNow > 0) {
      greeting = 'Good morning!';
    } else {
      greeting = 'Welcome!';
    }

    document.getElementById("greeting").innerHTML = greeting;

  }
}, false);

It is wonderful you use document.getElementById , instead of document.write !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM