This code should do the trick. Not tested though.
Note that neither of the scripts will work if the user has disabled javascript. It might also not work in some old browsers because of getElementById.
I'd want to run them onload since it might not catch all links/images otherwise.