Capturing mouse position with JavaScript

I needed to capture the (x, y) coordinates of the user's mouse when they clicked links in a grid. The original code used the clientX and clientY event properties, which worked fine until the grid grew enough rows that the page had to scroll. At that point those values were no longer relative to the position of the document.

The most useful reference I found on the topic was Peter-Paul Koch's write-up on event properties at QuirksMode.

The code I ended up with:

// determine x,y coordinates of mouse position relative to document
var clientX = 0;
var clientY = 0;

if (!e) var e = window.event;

if (e.pageX || e.pageY)
{
    clientX = e.pageX;
    clientY = e.pageY;
}
else if (e.clientX || e.clientY)
{
    clientX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
    clientY = e.clientY + document.body.scrollTop  + document.documentElement.scrollTop;
}

One note from the present day: pageX and pageY are supported everywhere now, so the fallback branch is only there for very old versions of Internet Explorer and can be dropped from new code.

Configure SQL Server Agent Mail to Use Database Mail

Email notifications stopped working for a SQL Server Agent job, and this was in the error log:

[264] An attempt was made to send an email when no email session has been established

The cause is that SQL Server Agent has its own mail settings, separate from Database Mail. Configuring Database Mail alone is not enough; Agent has to be told to use it.

To set up SQL Server Agent Mail to use Database Mail:

  1. In Object Explorer, expand a server.
  2. Right-click SQL Server Agent and click Properties.
  3. Click Alert System.
  4. Select Enable Mail Profile.
  5. In the Mail system list, select Database Mail.
  6. In the Mail profile list, select a mail profile for Database Mail.
  7. Restart SQL Server Agent.

That last step is the one people miss. The settings do not take effect until Agent restarts, so if you make the changes and the notifications still fail, that is usually why.