What does this code for?

Dimitrije
Visitor
1 0 0

Can someone explain this code function?

 

<script>
if(window.location.href.indexOf("merchantdomain.com") == -1) {
window.location = 'https://merchantdomain.com&#039;; }
</script>

 

 

Reply 1 (1)

Rishihuptech
Shopify Partner
351 63 68

HI @Dimitrije 
This script is used to ensure that a web page is only accessed from a specific domain, in this case, "merchantdomain.com". If someone tries to access the page from any other domain, they are automatically redirected to "merchantdomain.com".

  1. Checking the Current URL:

    • window.location.href refers to the URL of the current page.
    • indexOf("merchantdomain.com") checks if the string "merchantdomain.com" is part of the current URL. The indexOf method returns -1 if the string is not found.
  2. Redirection Logic:

    • if(window.location.href.indexOf("merchantdomain.com") == -1): This conditional statement checks if "merchantdomain.com" is not part of the current URL. If it isn't (i.e., indexOf returns -1), then the condition is true.
    • window.location = 'https://merchantdomain.com': If the condition is true (meaning the current page is not on "merchantdomain.com"), this line redirects the browser to 'https://merchantdomain.com'.
  3. Common Use Cases:

    • It's often used to enforce domain consistency, particularly for sites that may have multiple domains or subdomains.
    • It can also be a security measure to prevent phishing or unauthorized access to a web page from a different domain.
If you found this response helpful, please do like and accept the solution. Thanks!
Need support with Customizing your Shopify store?
Feel free to contact me at info@huptechweb.com or Visit our website Huptech Web.
Instant Shortcode Builder: Integrate customizable UI features anywhere in your store - No coding knowledge required