As far as I know there isn't a foolproof way to do it in Javascript yet - this question sets out two decent approaches, one which grabs the user agent string from the client and one that gets the device width. They each have pros and cons - user agent strings are subject to change, device width leaves you a bit open to confusion on mid-sized devices like tablets; depends on your use case which one works for you.
In terms of how to redirect desktop but not mobile users - both of these solutions are ways of detecting what the user's device is, then storing that information in a variable in Javascript. What you need to do from there is script that redirect behaviour for the group of users you want to move. Google if-statements and "window.location" and you'll find plenty of information out there on how to do that.