примерно:
window.onload = function() {
var element = document.getElementsByTagName('head')[0];
switch(/\/{2}(\d*\w*)/.exec(document.referrer)[1]) {
case 'google' :
element.innerHTML += '<script src=\"google.com\" type=\"text\/javascript\"><\/script>';
break;
case 'yandex' :
element.innerHTML += '<script src=\"yandex.com\" type=\"text\/javascript\"><\/script>';
break;
default :
element.innerHTML += '<script src=\"google.com\" type=\"text\/javascript\"><\/script>';
break;
}
};