Jump to content
Larry Ullman's Book Forums

Epoch.Js Excercise On Page 280


Recommended Posts

Hi all,

I have been working on the above excercise and cannot get it to work. According to the Firebug console 'U.$ is not a function ' on the following line of code:

 

U.addEvent(U.$('output'), 'mouseover', updateDuration);

 

I'll put all the code below but including the utilities.js file there's quite a bit of it. I suspect it may be the HTML file (it's the only one I had to do myself!). If it turns out to be a typo I shall go and stand in the corner, I've checked and rechecked the typing on all 3 files.

 

Cheers

Paul

 

First the HTML file:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>Epoch</title>

 

<link rel="stylesheet" href="default_CSS_stylesheet.css">

 

<script src="utilities.js"></script>

<script src="epoch.js"></script>

<!--[if lt IE 9]>

<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>

<![endif]-->

 

</head>

 

<body>

 

<p id="output"></p>

 

 

</body>

</html>

 

The epoch.js file:

 

// Javascript Document

function updateDuration()
{
'use strict';

var now = new Date();

var message = 'It has been ' + now.getTime();

message+= ' seconds since the epoch. (mouse over to update)';

U.setText('output', message);
}

window.onload = function()
{
'use strict'

U.addEvent(U.$('output'), 'mouseover', updateDuration);

updateDuration();
};

 

Finally the utilities.js file:

// Javascript Document

//Utilities script for Javascript
//To be added to every HTML page that requires Javascript

//Create one global object

var U =
{
//Define the $ method
//This method is used to return a document by id
$: function(id)
{
'use strict';
if (typeof id == 'string')
{
return document.getElementById(id);
}
},

//This method sets text in a HTML id
setText: function(id, message)
{
'use strict'
if ( (typeof id == 'string') && (typeof message == 'string') )
{
var output = this.$(id);

if (!output) return FALSE;

if (output.textContent !== undefined)
{
output.textContent = message;
}
else
{
output.innerText = message;
}

return TRUE;
}
},

//This method adds and removes event listeners for browsers and IE prior to IE9
addEvent: function(obj, type, fn)
{
'user strict'
if (obj && obj.addEventListener)
{
obj.addEventListener(type, fn, false);
}
else if (obj && obj.attachEvent)
{
obj.attachEvent('on' + type, fn);
}
},

removeEvent: function(obj, type, fn)
{
'user strict'
if (obj && obj.removeEventListener)
{
obj.removeEventListener(type, fn, false);
}
else if (obj && obj.detachEvent)
{
obj.detachEvent('on' + type, fn);
}
}

};

var U = { /* functions */};

Link to comment
Share on other sites

Larry

Thanks for that, at least I can stay out of the corner for typos, although arguably I should be there for lack of common sense. That's what happens when you copy without understanding. Sorry to have bothered everyone.

 

Paul

Link to comment
Share on other sites

 Share

×
×
  • Create New...