6

I need to get an overlay to slide down based on what the URL has at the end of it.

If (URL has 'faq' at the end) { overlay comes down }

How can you do that in jQuery/JavaScript?

7

4 Answers 4

16

If your URL looks something like this http://yourdomain.com/faq, you could do something like this:

var url = window.location.href;
var lastPart = url.substr(url.lastIndexOf('/') + 1);

if (lastPart === "faq") {
   // Show your overlay
}

This would make it possible to check for other endings and act on them as well.

Update:

To get it working, even if the URL has a trailing slash, you could create a function like this:

function getLastPart(url) {
    var parts = url.split("/");
    return (url.lastIndexOf('/') !== url.length - 1 
       ? parts[parts.length - 1]
       : parts[parts.length - 2]);
}

You could then call the function like getLastPart(window.location.href) to get the last part of the URL for the current page.

Here is a working example as well: http://jsfiddle.net/WuXHG/

Disclaimer: If your URLs use hashes at the end, or a querystring, you would have to strip the from the URL first, for this script to work properly

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

2 Comments

@Alex Correct, in that case it wouldn't work. Does your addresses always have a trailing /, or is it only some paths that have it?
thumbs up for this line url.lastIndexOf('/') !== url.length - 1
11

You should be able to use the window.location object for this with a regexp, something like this:

/faq$/.test(window.location)

If you want to match just the path regardless of query string or hash:

/faq$/.test(window.location.pathname)

1 Comment

To ignore parameters: /path\/to$/.test(window.location.pathname)
6

A new method endsWith() has been added to the ES6 specification. For previous versions we can polyfill it using

if (!String.prototype.endsWith)
  String.prototype.endsWith = function(searchStr, Position) {
      // This works much better than >= because
      // it compensates for NaN:
      if (!(Position < this.length))
        Position = this.length;
      else
        Position |= 0; // round position
      return this.substr(Position - searchStr.length,
                         searchStr.length) === searchStr;
  };

Now you can easily write

If (window.location.href.endsWith("faq")) { 
   // Show your overlay
}

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith

Comments

1

You can get the current URL using :

 var currentUrl = window.location.href;

Then you can use indexOf to check if your token is in the end of string (here faq)

 if (currentUrl.indexOf('faq') == currentUrl.length - 3)
 {
  // Do something here
 }

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.