0

So basically, I want to only call some script if the URL does not equal blogs.html

For example, these parameters should NOT call the script:

mydomains.com/blogs
mydomains.com/blogs.html

This parameters should call the script:

mydomains.com/blogs/child-page.html
mydomains.com/blogs/another-page
mydomains.com/blogs.html/testin-page.html
mydomains.com/page.html
mydomains.com

I have attempted something like this, although it does not seem to work since blogs.html and blogs are still within the URL.

if(!document.URL.indexOf("blogs.html") >= 0 && !document.URL.indexOf("blogs") >= 0)
{ 
other script here
}

Is there any way that I can fix this?

5
  • Is there a reason you have to use js to do this? Commented Dec 2, 2013 at 0:59
  • If you are OK with using a plugin for this; there is a great one. You may want to check this out. js-url Commented Dec 2, 2013 at 1:01
  • @Daedalus - No, not at all. Open to PHP or other solutions. Just thought that maybe Javascript may have been the simplest solution. Commented Dec 2, 2013 at 1:02
  • @alix - Not really necessary since within my if statement, there will only be 1 tiny bit of code that will be blocking a div. Commented Dec 2, 2013 at 1:03
  • why look for what isn't and simply set server to identify that it is with a variable...or only include the script on that page Commented Dec 2, 2013 at 1:09

2 Answers 2

1

First get the pathname, then use the substring method to get everything after the last "/".

var pathName = window.location.pathname;
var pageName = pathName.substr( pathName.lastIndexOf("/") + 1 );

if( pageName != "blogs.html" && pageName != "blogs" ) {
    // do something.
}
Sign up to request clarification or add additional context in comments.

Comments

1
if(!document.URL.endsWith("blogs.html"))
{ 
 // code here
}

string has endsWith() methods to accomplish this goal

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FString%2FendsWith

or use a regular expression: $

if(!document.URL.search(/(blogs.html)$/) != -1) 
// return -1 means a string is not end with blogs.html
{ 
 // code here
}

http://jsfiddle.net/rchMe/

Comments

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.