Skip to content

Instantly share code, notes, and snippets.

@oliverstech
Created October 6, 2021 16:59
Show Gist options
  • Save oliverstech/f27297474eb9ebc4fd25cf1664264a76 to your computer and use it in GitHub Desktop.
Save oliverstech/f27297474eb9ebc4fd25cf1664264a76 to your computer and use it in GitHub Desktop.
Javascript - Redirecting to a domain
<html>
<script>
var path = window.location.pathname;
window.location.href = "https//www.example.com" + path;
</script>
</html>
<!--Here's a demo of what I mean-->
<html>
<script>
// Our js goes here
// Store the path in a variable
// Example: /wmc
var path = window.location.pathname;
// Redirect user to website + path
// Example: https://www.example.com/wmc
window.location.href = "https://www.example.com" + path; // A forward slash isn't needed after the domain.
// Example Result: Now, upon visit, the user will be redirected to: 'https://www.example.com/wmc'
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment