+6  A: 

You're passing in a function where a string is expected. Try this instead:

    var imgUrl = 'http://sstatic.net/so/img/logo.png';
    var img = document.createElement('img');
    img.src = imgUrl;
    img.onmouseover = function() {
     alert('mouseover ' + imgUrl);
    };
    document.body.appendChild(img);
Nosrama
it works, but I'm still not exactly sure why, what did you change?
CrazyJugglerDrummer
The function was assigned directly to the method, not via the setAttribute function, where a string is expected. A much cleaner solution than mine. +1
Kyle Rozendo
@CrazyJugglerDrummer: the setAttribute method takes 2 strings - so you could've used that method but you would have had to wrap the function in double-quotes and this could also have caused problems accessing the imgUrl value as now it would be looked for in global scope.
Nosrama
@CrazyJugglerDrummer: If Nosrama's answer helped you, please accept his answer. Not only does it enlarge his e-peen, but it helps people with the same question get the answers they need, faster :)
roosteronacid