AJAX - The Complete Reference

(avery) #1

Chapter 2: Pre-Ajax JavaScript Communications Techniques 55


PART I


else if (window.attachEvent)
window.attachEvent("onload", AjaxTCRExamples.regLinks);

The following XHTML document shows how it might be used. You can view it live at
http://ajaxref.com/ch2/recordlink.html.

<!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=iso-8859-1" />
<title>Chapter 2 : Track Outbound Links</title>
<script src="http://ajaxref.com/ch2/outboundtrack.js"
type="text/javascript"></script>
</head>
<body>
<h1>Outside Links</h1>
<hr />
<a href="http://www.ajaxref.com">Ajax - The Complete Reference</a> <em>Internal
link - not logged</em><br />
<a href="http://www.yahoo.com">Yahoo!</a><br />
<a href="http://www.google.com">Google</a><br />
<a href="http://www.ucsd.edu">UCSD</a><br />
<a href="http://www.pint.com">PINT</a><br />
<hr />
<a href="http://ajaxref.com/ch2/outlinks.txt">View logged links</a><br />
</body>
</html>

Notice once again we have employed some JavaScript techniques such as object wrappers
and a safe window.onload handler so you could potentially use the link tracker in your own
projects without interfering with any scripts you may have already. You simply need to set the
URL for the server-side recording script you want to use. A simple example of the server-side
code to record the link presses is very similar to previously presented examples and is again
omitted for brevity, but it can be found on the book support site ajaxref.com.
There is one aspect of the client code that should be noted: the use of the mousedown
events as opposed to click events. You may try to substitute click events but you’ll find that
occasionally the browser will not get the outbound link recorded before the current page is
torn down. It is apparently a necessary hack and it is used on other link monitor scripts
found online.

Fancy Ratings—Pre-Ajax Version

Given that you have seen the rating example so many times, you might want to see one
done in a bit more pleasing style, as shown in Figure 2-11 and available live at http://
ajaxref.com/ch2/rating.html. If you inspect the code here, notice that it contains a significant
amount of code to handle the dynamic effects of the animated stars.

<!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">
Free download pdf