4

I'd like to execute the function up(); if the url contains "invt".

So far I have this:

if(window.location.pathname == "/invt/"){
  alert("invt");
}

However, this wasn't alerting for me. Any ideas?

I've since got this to work, thanks to the answers below.

Consider:

$(document).ready(function () {
   if(window.location.href.indexOf("invt") > -1) { //checks url for "invt" (all products contain this)
     up();
     return;
       }
      });
2
  • That's not contains - that means if it is "/invt/" with no text before or after. See answers below for usage of .indexOf() Commented Sep 3, 2013 at 14:46
  • Thanks. Didn't realize this, I've since got it to work! Commented Sep 3, 2013 at 14:49

2 Answers 2

3

Try this:

if ( window.location.pathname.indexOf('invt') >= 0 )
{
 alert("invt");
}
Sign up to request clarification or add additional context in comments.

Comments

3

You can use indexOf():

if (-1 != location.pathname.indexOf('invt')) {
    alert ("GO");
}

Demo

Try before buy

2 Comments

Thanks :-) Very useful! I only accepted dwaddell's because he was sightly faster, you both provided more than acceptable answers. :)
You're very welcome. That's perfectly OK: Always select the most suitable answer. It's not a race. :)

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.