(Moved to web programming)
It is fairly easy to pop up the window using on onunload event in the body tag, however, to randomize the display of this popup would require some server-side programming. You would use PHP or SSI to add the javascript code to the pages of the user you want to question. So if you put the javascript code into a text file (lets say, whydyaleave.php) you might include the following in your header:
PHP Code:
<? $r = rand(1,10); if ($r == 2) include(whydyaleave.php); ?>
And the following to your body tag:
PHP Code:
<body<?=($r==2)?' onunload="loadJavascript"':''?>">
This would cause each page to have a 1 in 10 chance of displaying the popup if someone left.
Extra credit to anyone who names the obscure and ancient decision logic used in the body tag.