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.