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?
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?
If your URL looks something like this
http://yourdomain.com/faq
, you could do something like this: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:
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
You can get the current URL using :
Then you can use indexOf to check if your token is in the end of string (here faq)
You should be able to use the window.location object for this with a regexp, something like this:
A new method
endsWith()
has been added to the ES6 specification. For previous versions we can polyfill it usingNow you can easily write
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith