Route to another page conditionally

I need to route from existing page on button click(from javascript) based on conditions returned from another database(hosted some where else). Like for example getting roles from database and based on the roles redirect to the page.