0

I want to run javascript on specific URL load, below is my code. It only shows an alert on the home page, not on the URL I have a condition on i.e. https://www.example.com/cart it gives an alert message on the home page only.

<script type="text/javascript">
    var currURL = window.location.href;

   function codeAddress() {

     if (currURL = 'https://www.example.com/cart'){
            alert('We are not shipping to countries that are not listed in shipping country!');
     }
    }
  window.onload = codeAddress;
        </script>

1 Answer 1

3

The line

if (currURL = 'https://www.example.com')

should be

if (currURL === 'https://www.example.com')

Remember that,

  1. = is assignment, not comparison.
  2. == is comparison without type check.
    1 == '1'  // true
    1 == 1    // true
    
  3. === is comparison with type check.
    1 == '1'  // false
    1 == 1    // true
    
Sign up to request clarification or add additional context in comments.

1 Comment

I was looking at this code trying to find the problem for 5 minutes and still didn't realize where was the mistake bruuh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.