Try using the standard array Links instead of calling getElementsByTagName(). However, I doubt that is the cause of the problem.
You need to break up the code and use alert() to find out where it's going wrong. However, if Safari supports it, try either turning on error reporting or opening a javscript console to (view error messages). I've never used Safari but it should support one of these.
It's also possible that the code is fine but you need to change a setting on Safari.