Solved

javascript

Posted on 2007-03-19
7
1,212 Views
Last Modified: 2010-08-05
I am learning java script. I have an external .js script and an embedded script in my files. It is not showing the date values or the comments.  I have the external file and then the .htm file below. Does the order need to be different?

.js-javascrip filet--

function XmasDays(thisDate) {
   var XMYear=thisDate.getFullYear();
   var XMDay=new Date("December, 25, 2005");
   XMDay.setFullYear(XMYear);
   var dayTotal=(XMDay-thisDate)/(1000*60*60*24);
   dayTotal=Math.round(dayTotal);
   return dayTotal;
}

function showDate(thisDate) {
var thisWDay=thisDate.getDay();
var thisDay=thisDate.getDate();
var thisMonth=thisDate.getMonth();
var thisYear=thisDate.getYear();
var monthName=new Array(12);
monthName[0]="January";
monthName[1]="February";
monthName[2]="March";
monthName[3]="April";
monthName[4]="May";
monthName[5]="June";
monthName[6]="July";
monthName[7]="August";
monthName[8]="September";
monthName[9]="October";
monthName[10]="November";
monthName[11]="December";
var wdayName=new Array(7);
wdayName[0]="Sunday";
wdayName[1]="Monday";
wdayName[2]="Tuesday";
wdayName[3]="Wednesday";
wdayName[4]="Thursday";
wdayName[5]="Friday";
wdayName[6]="Saturday";
dateString="weekday, month day, year";
document.write(wdayName[thisWDay]+", "+monthName[thisMonth]+" "+thisDay+", "+thisYear);  
}

------------------------------------------------------------
.htm file--
<html>
<head>

<title>North Pole Novelties</title>
<link href="styles2.css" rel="stylesheet" type="text/css" />

<script type="text/javascript"src="library2.js">showDate();</script>

</head>

<body>
<table id="npn" cellpadding="5" cellspacing="0">
<tr>
<!-- Company Hyperlinks -->
<td id="links" rowspan="4" valign="top">
   <a href="#">Home Page</a>
   <a href="#">Shopping Cart</a>
   <a href="#">Your Account</a>
   <a href="#">Contact Us</a>
   <a href="#" class="newgroup">Angels</a>
   <a href="#">Cards</a>
   <a href="#">Collectibles</a>
   <a href="#">Creches</a>
   <a href="#">Garland</a>
   <a href="#">Gift Wraps</a>
   <a href="#">Lights</a>
   <a href="#">Nutcrackers</a>
   <a href="#">Ornaments</a>
   <a href="#">Santas</a>
   <a href="#">Trains</a>
   <a href="#">Trees</a>
   <a href="#">Villages</a>
   <a href="#">Wreaths</a>
</td>

<!-- Company Logo -->
<td id="logo">
   <img src="logo.jpg" alt="North Pole Novelties" />
</td>
<!-- Days until Christmas -->
<td id="daycell">

<script language="JavaScript">
<!--HIDE FROM INCOMPATIBLE BROWSERS
function date() {
var today="December 24,2007";
var niceDate=today.showDate();
var daysleft=today.XMasDays();
document.write(niceDate +"<br/>");

if(daysLeft>1)  {

document.write("Only", +daysLeft + "days until Christmas")

} else if (daysLeft==1) {

document.write("Last Day for Christmas Shopping")

}
else {

document.write.("Happy Holidays from North Pole Novelties")

}

//STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>

</td>
</tr>

<tr>
<!-- Articles about the company -->
<td colspan="2">
   <table cellspacing="4" cellpadding="4">
   <tr>
   <!-- Welcome message -->
   <td id="firstcol" valign="top">
      <p class="firstp"><img src="bells.jpg" alt="" style="float: left" />
      Welcome to our online store. Consider us your complete holiday
      store. Whether you're a collector or simply looking for a beautiful
      piece to treasure for years, you'll find it at NPN. Please click on
      one of the many links to explore all we have to offer.</p>

   <!-- Latest news -->
      <h3>News Flash!</h3>
      <p>North Pole Novelties is proud to announce a new line of
      <b>Lasseter Old Towne Village collectible houses</b>.
      Start building your collection of these wonderful miniature
      porcelain houses and shops. </p>
      <p>Each model has accessories to enhance the collection. Be sure to
      order extra trees, fences, street lights and signs to bring activity
      and a festive atmosphere to your miniature town.</p>
   </td>

   <!-- Information about the company -->
   <td id="secondcol" valign="top">
      <h3>Who Are We?</h3>
      <p>
      <img src="watkins.jpg" alt="" style="float: right; margin: 0px 0px 15px 15px" />
      Located in Seton Grove, Minnesota, North Pole Novelties is one of the
      oldest and largest holiday stores in the country. The store was founded
      in 1968 by <b>David Watkins</b> (shown here). Today, David, his family,
      and over 300 employees manage the daily operation of making the holiday
      season last all year.</p>
      <p>The store itself is the size of two football fields and attracts
      visitors from around the world. If you can't pay us a visit, order our
      catalogue. We will deliver to any spot in the United States and
      overseas. In a rush? We can deliver your order overnight! We also
      gift wrap.</p>
   </td>
   </tr>
   </table>
</td>
</tr>

<tr>
<!-- Contact Information -->
<td id="contact" colspan="2">
   <b>North Pole Novelties</b> |
   25 Oakdale Avenue |
   Seton Grove, MN 53112 |
   (404) 555-1225
</td>
</tr>
</table>

</body>
</html>

0
Comment
Question by:pgmtkl
7 Comments
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 250 total points
ID: 18748548
First of all is this NOT valid HTML tag usage:
<script type="text/javascript"src="library2.js">showDate();</script>

THis is correct:
<script type="text/javascript"src="library2.js"></script>




0
 
LVL 63

Expert Comment

by:Zvonko
ID: 18748719
The rest of your scripts is full of errors. Even when errors are corrected is the logic of the script returning same day for actual day and christmass.

If it helps then check this:
library2.js:
function XmasDays(thisDate) {
   var XMYear=thisDate.getFullYear();
   var XMDay=new Date("December, 25, 2005");
   XMDay.setFullYear(XMYear);
   var dayTotal=(XMDay-thisDate)/(1000*60*60*24);
   dayTotal=Math.round(dayTotal);
   return dayTotal;
}

function showDate(thisDate) {
var thisWDay=thisDate.getDay();
var thisDay=thisDate.getDate();
var thisMonth=thisDate.getMonth();
var thisYear=thisDate.getYear();
var monthName=new Array(12);
monthName[0]="January";
monthName[1]="February";
monthName[2]="March";
monthName[3]="April";
monthName[4]="May";
monthName[5]="June";
monthName[6]="July";
monthName[7]="August";
monthName[8]="September";
monthName[9]="October";
monthName[10]="November";
monthName[11]="December";
var wdayName=new Array(7);
wdayName[0]="Sunday";
wdayName[1]="Monday";
wdayName[2]="Tuesday";
wdayName[3]="Wednesday";
wdayName[4]="Thursday";
wdayName[5]="Friday";
wdayName[6]="Saturday";
dateString="weekday, month day, year";
 return (wdayName[thisWDay]+", "+monthName[thisMonth]+" "+thisDay+", "+thisYear);  
}


html:
html>
<head>

<title>North Pole Novelties</title>
<link href="styles2.css" rel="stylesheet" type="text/css" />

<script type="text/javascript"src="library2.js" ></script>

</head>

<body>
<table id="npn" cellpadding="5" cellspacing="0">
<tr>
<!-- Company Hyperlinks -->
<td id="links" rowspan="4" valign="top">
   <a href="#">Home Page</a>
   <a href="#">Shopping Cart</a>
   <a href="#">Your Account</a>
   <a href="#">Contact Us</a>
   <a href="#" class="newgroup">Angels</a>
   <a href="#">Cards</a>
   <a href="#">Collectibles</a>
   <a href="#">Creches</a>
   <a href="#">Garland</a>
   <a href="#">Gift Wraps</a>
   <a href="#">Lights</a>
   <a href="#">Nutcrackers</a>
   <a href="#">Ornaments</a>
   <a href="#">Santas</a>
   <a href="#">Trains</a>
   <a href="#">Trees</a>
   <a href="#">Villages</a>
   <a href="#">Wreaths</a>
</td>

<!-- Company Logo -->
<td id="logo">
   <img src="logo.jpg" alt="North Pole Novelties" />
</td>
<!-- Days until Christmas -->
<td id="daycell">

<script language="JavaScript" >
<!--HIDE FROM INCOMPATIBLE BROWSERS
function date() {
  var today= new Date("December 24,2007");
  var niceDate = showDate(today);
  var daysLeft = XmasDays(today);
  document.write(niceDate +"<br/>");
  if(daysLeft>1)  {
    document.write("Only", +daysLeft + "days until Christmas")
  } else if (daysLeft==1) {
    document.write("Last Day for Christmas Shopping")
  } else {
    document.write("Happy Holidays from North Pole Novelties")
  }
}

date();
//STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>

</td>
</tr>

<tr>
<!-- Articles about the company -->
<td colspan="2">
   <table cellspacing="4" cellpadding="4">
   <tr>
   <!-- Welcome message -->
   <td id="firstcol" valign="top">
      <p class="firstp"><img src="bells.jpg" alt="" style="float: left" />
      Welcome to our online store. Consider us your complete holiday
      store. Whether you're a collector or simply looking for a beautiful
      piece to treasure for years, you'll find it at NPN. Please click on
      one of the many links to explore all we have to offer.</p>

   <!-- Latest news -->
      <h3>News Flash!</h3>
      <p>North Pole Novelties is proud to announce a new line of
      <b>Lasseter Old Towne Village collectible houses</b>.
      Start building your collection of these wonderful miniature
      porcelain houses and shops. </p>
      <p>Each model has accessories to enhance the collection. Be sure to
      order extra trees, fences, street lights and signs to bring activity
      and a festive atmosphere to your miniature town.</p>
   </td>

   <!-- Information about the company -->
   <td id="secondcol" valign="top">
      <h3>Who Are We?</h3>
      <p>
      <img src="watkins.jpg" alt="" style="float: right; margin: 0px 0px 15px 15px" />
      Located in Seton Grove, Minnesota, North Pole Novelties is one of the
      oldest and largest holiday stores in the country. The store was founded
      in 1968 by <b>David Watkins</b> (shown here). Today, David, his family,
      and over 300 employees manage the daily operation of making the holiday
      season last all year.</p>
      <p>The store itself is the size of two football fields and attracts
      visitors from around the world. If you can't pay us a visit, order our
      catalogue. We will deliver to any spot in the United States and
      overseas. In a rush? We can deliver your order overnight! We also
      gift wrap.</p>
   </td>
   </tr>
   </table>
</td>
</tr>

<tr>
<!-- Contact Information -->
<td id="contact" colspan="2">
   <b>North Pole Novelties</b> |
   25 Oakdale Avenue |
   Seton Grove, MN 53112 |
   (404) 555-1225
</td>
</tr>
</table>
</body>
</html>


0
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 250 total points
ID: 18748730
Now I see what you are doing!!!
Check this html script section:
<script language="JavaScript" >
<!--HIDE FROM INCOMPATIBLE BROWSERS
function date() {
  var today= new Date();
  var niceDate = showDate(today);
  var daysLeft = XmasDays(today);
  document.write(niceDate +"<br/>");
  if(daysLeft>1)  {
    document.write("Only", +daysLeft + "days until Christmas")
  } else if (daysLeft==1) {
    document.write("Last Day for Christmas Shopping")
  } else {
    document.write("Happy Holidays from North Pole Novelties")
  }
}

date();
//STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>


0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 11

Expert Comment

by:flipz
ID: 18748736
You are also not calling the date() function (which I would rename to something like displayDate() anyway). If you just want to function to fire right away then put the line date(); (or displayDate(); if you rename the function) just before your closing script tags like this:


displayDate();
//STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>

There may be other issues - post your results after making the changes (including Zvonko's).
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 250 total points
ID: 18749152
So script:
function formatDate() {
  var today= new Date();
  var niceDate = showDate(today);
  var daysLeft = XmasDays(today);
  document.write(niceDate +"<br/>");
  if(daysLeft>1)  {
    document.write("Only ", +daysLeft + " days until Christmas")
  } else if (daysLeft==1) {
    document.write("Last Day for Christmas Shopping")
  } else {
    document.write("Happy Holidays from North Pole Novelties")
  }
}

function XmasDays(thisDate) {
   var XMYear=thisDate.getFullYear();
   var XMDay=new Date(XMYear,11,25);
   var dayTotal=(XMDay-thisDate)/(1000*60*60*24);
   dayTotal=Math.round(dayTotal);
   return dayTotal;
}
var monthName="January,February,March,April,May,June,July,August,September,October,November,December".split(',');
var wdayName="Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday".split(',');
function showDate(thisDate) {
  var thisWDay=thisDate.getDay();
  var thisDay=thisDate.getDate();
  var thisMonth=thisDate.getMonth();
  var thisYear=thisDate.getYear();
  return (wdayName[thisWDay]+", "+monthName[thisMonth]+" "+thisDay+", "+thisYear);  
}



and where you want it:

<script language="JavaScript" >
<!--HIDE FROM INCOMPATIBLE BROWSERS
formatDate();
//STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>
0
 

Author Comment

by:pgmtkl
ID: 18749527
Thanks. It is working when i changed the embedded script code below.  In the top hand of the page when i run it it displays as "Monday, March 19, 2007undefined" . Everything is defined, where is this error coming from?


<script language="JavaScript" >
<!--HIDE FROM INCOMPATIBLE BROWSERS
function date() {
  var today= new Date();
  var niceDate = showDate(today);
  var daysLeft = XmasDays(today);
  document.write(niceDate +"<br/>");
  if(daysLeft>1)  {
    document.write("Only", +daysLeft + "days until Christmas")
  } else if (daysLeft==1) {
    document.write("Last Day for Christmas Shopping")
  } else {
    document.write("Happy Holidays from North Pole Novelties")
  }
}

date();
//STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>
0
 

Author Comment

by:pgmtkl
ID: 18749587
I found what was wrong, a return statement. works now. thanks for the help.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question