Here javascript may well be the worst of all possible approaches-- and that's speaking as someone who uses js as a word processor, as a generic programming tool, and doubtless many other things that God never intended js to do.
By its nature javascript doesn't execute until it arrives in the user's browser. So your mobile user ends up having to download everything twice-- whether they want to or not, even if they're on a plan that makes you pay through the nose for data.
If you must use UA detection, do it on the server side so the user never even loads up the full-scale version. But why not offer the user a choice? Link to "our mobile site" and remember this decision in a cookie for future visits.
whether or not it could cause problems with older browsers
You mean, ancient desktop browsers running in machines narrower than 499px? I don't think they exist; the original Mac was 512x384 and that takes us back a good many decades. Besides, browsers that old probably don't even understand "screen.width". Got a vague idea MSIE 7 didn't.
And, ahem, you don't really mean /index.html ;) On two accounts, come to think of it. Would this script appear on every page? Then they need to get redirected to the mobile version of the current page, not to the front page all over again. I think there's even something in google's guidelines about this.