Ant
Ant

Reputation: 42

using jquery to detect URL and change HTML

Our client has two URL's that point to the same page. Depending on which URL the user comes through they want to display and hide certain content. I have the following code and everything looks like it should work (doesn't it always....) but for some reason the if doesn't evaluate to true. The alert is in there for troubleshooting purposes.

var this_page = window.location;
var calc_address = "DIFFERENT ADDRESS";

alert(this_page);


      if(this_page == "http://www.calculatesnowguards.com/"){
          $('#mashead').css('background-image', 'url("../images/masthead_bg.jpg") ');
          $('.calc_remove').hide();
          $('#bottom').innerHTML = calc_address;
    }

Upvotes: 0

Views: 231

Answers (1)

GJK
GJK

Reputation: 37369

window.location is not a string, it's only represented as so. It's actually an object. window.location.href is the variable you want to compare to.

EDIT: (In response to the comments below.) With such different URLs, why would you try to compare them directly?

if (window.location.href.indexOf("calculatesnowguards.com") >= 0) {
    //code for calculatesnowguards.com
} else{
    //code for snowguards.biz
}

EDIT2: Sorry, didn't realize that contains() was a Firefox only function. I extend String to include it in my scripts.

Upvotes: 3

Related Questions