0

Please have a look at the following:

jQuery(document).ready(function() { 
    var full_url = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname;    
    var part_url = window.location.protocol + "//" + window.location.host + "/domain/shop/";    
    part_url = part_url + "cat1/";

    if (full_url == part_url)
    {
        jQuery("li.cat1").addClass("current");  
    }    
});

For some reason the above code never becomes true and I am puzzled as why. I have individually alerted out the variable "part_url" and "full_url" and they both matched so why is the if statement not returning true? I know it's probably something silly that I've missed...

2
  • /domain/shop/cat1 == window.location.pathname ? (the last slash can be a problem btw) Commented Jul 2, 2010 at 17:20
  • You know the first line: jQuery(document).ready(function() { could be shortened to $(function() { Commented Feb 14, 2012 at 8:10

3 Answers 3

1

Are you sure it's not returning true? Try:

alert(full_url == part_url);

That will give some indication as to whether the strings don't match, or whether the content of the if is just having no effect.

If it's a lower/upper case issue, then it's better to use toLowerCase() to compare, rather than regular expressions, as otherwise you can get errors / false matches (if the URLs contain regexp characters):

if (full_url.toLowerCase() == part_url.toLowerCase()) { ... }

If that's not it - try:

  • checking the lengths of the strings:

    alert(full_url.length); alert(part_url.length);
    
  • using substr on the strings to narrow down the part that doesn't match:

    alert(full_url.substr(1,20) == part_url(substr(1,20));
    

    (maybe even use a loop to compare character by character - it may be something like number '1' versus letter 'l'...)

Sign up to request clarification or add additional context in comments.

2 Comments

Hi psmears, I did as you suggested and got some strange findings.. I did the alert length on both variables and they were different 56 and 57 BUT here is the kicker, I then alert both the variables back to back and the string is identical...So I am lost as to why the length is different and why it is not equal...I checked all cases and they are all lower so thats not it..Any more ideas?
@Rick: Try the 'substr' thing - it's possible one of the strings has somehow obtained an 'invisible' character. This will show up, for example, if you alert with the first 5 (say) characters: if you get 'http:' and 'http' then you'll know one has an invisible control character at/near the start. And, as someone else has said, check for the '/' at the end :)
0

if the equality operator says false, then they're not the same no matter how much you believe it. ;) try this:

var test = function (s, t)
{
    var sl = s.length;
    var tl = t.length;
    var l  = sl < tl ? sl : tl;
    for (var i = 0; i < l && s[i] == t[i]; ++i) /* empty */;
    if (i < l || sl != tl) {
        print("common head: '" + s.substr(0, i) + "'");
        print("tail s: '" + s.substr(i) + "'");
        print("tail t: '" + t.substr(i) + "'");
    }
};

Comments

0
window.location.host + "/" + window.location.pathname; 

location.pathname includes the leading /, so you'll be creating http://www.example.com//domain/shop/cat1 with two slashes after the hostname. Clearly this will never match. Refine your squinting-at-strings-to-see-if-they're-the-same powers!

1 Comment

I found this after I posted my comment so you're right and you're also wrong! true that is already has the slash but if you alert it, you will quickly notice it strips that slash out so it won't show // as you think it would or should...that is why I was not able to figure it out by looking...

Your Answer

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