0

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;
    }
5
  • 1
    I prefer console.log over alert. =) Commented Mar 14, 2013 at 19:32
  • this will only work on the root, where the page is not specified. Commented Mar 14, 2013 at 19:33
  • Suggest you build the page differently server-side rather than adapting it client-side. Commented Mar 14, 2013 at 19:41
  • why don´t you use this_page.indexof("url between www. and .com") > -1{what to do} Commented Mar 14, 2013 at 19:42
  • The only pages I need to adjust are in the root. Also, I would have rather did this server-side but it was a last minute change and would take to long to rebuild. Or so I thought.... :-/ Commented Mar 14, 2013 at 20:04

1 Answer 1

3

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.

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

7 Comments

More likely than not, the address it gives you is not going to be exactly what you want. You need to use of regular expressions or some other kind of search. Even if you get it to work in one browser, that's not a guarantee it'll work in all of them. What are the 2 URLs that link to this page?
calculatesnowguards.com and snowguards.biz
I made the switch and get the following in Chrome: Uncaught TypeError: Object calculatesnowguards.com has no method 'contains'
I fixed my error. But really, we're not here to write code for you. We're here to help with nontrivial issues. Searching for that error or the contains function would have given you your answer in about 2 minutes. It makes me a little unwilling to help when somebody can't take 5 minutes of their time to try to help themselves before asking others to help them.
indexOf returns - 1 if the value to search for never occurs use if (window.location.href.indexOf("calculatesnowguards.com") > -1)
|

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.