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
    
1
  • 1
    I was looking at this code trying to find the problem for 5 minutes and still didn't realize where was the mistake bruuh
    – smunteanu
    Commented Aug 29, 2020 at 22:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.