To test if a user is logged in use is_user_logged_in()
.
To redirect a visitor use wp_redirect($location, $status = 302)
.
You can combine both:
<?php
$location = 'http://localhost';
if ( is_user_logged_in() )
$location = admin_url( "https://wordpress.stackexchange.com/" );
wp_redirect( $location );
In JavaScript (sort of):
add_action( 'wp_footer', 'wpse_73701_button' );
function wpse_73701_button()
{
$location = is_user_logged_in() ? admin_url( "https://wordpress.stackexchange.com/" ) : 'http://localhost';
print "<button onclick='window.location.href=\"$location\"'>Click!</button>";
}
You could also just create JavaScript variables and use them in your scripts:
<script>
var isLoggedIn = <?php print is_user_logged_in() ? 'true' : 'false'; ?>;
var redirectTo = '<?php print is_user_logged_in() ? admin_url( "https://wordpress.stackexchange.com/" ) : 'http://localhost'; ?>';
</script>