user1228517
user1228517

Reputation:

Javascript working in JS Fiddle but not in my Dreamweaver Environment (via preview in browser Safari or Opera or Live View)

I came across the script online and have slightly modified it to print out the current date and time in a customised manner. It works fine in JS Fiddle http://jsfiddle.net/PVZZ8/ but in my HTML document it doesn't do a thing. It won't work in Live View nor when I preview the pages in Safari or Opera. I have checked over the syntax and all seems good. I am using dreamweaver CS5. I have used some shorter scripts I found on W3 Schools and they work fine but I am quite new to Javascript so perhaps there is some general step I am missing in the longer script I am using? The HTML document looks like this:

 <!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=UTF-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js">
</script>
<script type="text/javascript">
function getCalendarDate()
{
   var months = new Array(13);
   months[0]  = "JAN";
   months[1]  = "FEB";
   months[2]  = "MAR";
   months[3]  = "APR";
   months[4]  = "MAY";
   months[5]  = "JUN";
   months[6]  = "JUL";
   months[7]  = "AUG";
   months[8]  = "SEP";
   months[9]  = "OCT";
   months[10] = "NOV";
   months[11] = "DEC";
   var now         = new Date();
   var monthnumber = now.getMonth();
   var monthname   = months[monthnumber];
   var monthday    = now.getDate();
   var year        = now.getYear();
   if(year < 2000) { year = year + 1900; }
   var dateString = monthday +
                    ' ' +
                    monthname +
                    ' ' +
                    year;
   return dateString;
} 

function getClockTime()
{
   var now    = new Date();
   var hour   = now.getHours();
   var minute = now.getMinutes();
   var second = now.getSeconds();
   var ap = "AM";
   if (hour   > 11) { ap = "PM";             }
   if (hour   > 12) { hour = hour - 12;      }
   if (hour   == 0) { hour = 12;             }
   if (hour   < 10) { hour   = "0" + hour;   }
   if (minute < 10) { minute = "0" + minute; }
   if (second < 10) { second = "0" + second; }
   var timeString = hour +
                    ':' +
                    minute +
                    ':' +
                    second +
                    " " +
                    ap;
   return timeString;
}

var calendarDate = getCalendarDate();
var clockTime = getClockTime();
window.onload = document.write('<p>' + 'Your fortunes as of' + ' ' + calendarDate + ' ' + ' ' + 'at' + ' ' + clockTime + '</p>');​​​​​​​​​​
</script>

</head>

<html>
<body>


</body>
</html> 

Upvotes: 0

Views: 556

Answers (1)

Danilo Celic
Danilo Celic

Reputation: 2467

When I copy and pasted your code above and tired it within Dreamweaver, as well as in IE9 and FIrefox and all errored out. I checked in the console for IE9 and Firefox and both indicated an illegal character after the semi-colon at the end of the last line (the document.write line). In checking the source for the code above, I see that there is a string of 10 "&#8203" characters ( zero-width space ) after that last semi-colon. Not sure how they got there, but once I removed them from my code the page began working in IE9, Firefox and Dreamweaver's Live View.

To do this I basically had to select the trailing semi-colon and the opening < character, delete them and re-type them in order to delete the zero width space characters.

FWIW: Dreamweaver's Show Hidden Characters didn't show these characters. Although thinking about it, not sure how it would show something that's zero width anyway, but it would be good if there were some sort of indicator for such characters. I'll log a bug at: http://adobe.ly/DWwish please do so as well, the more people that log it the more chance it'll get implemented.

Upvotes: 1

Related Questions