我需要一个覆盖依据是什么网址具有在它的结束滑下。
如果(URL在末端“常见问题解答”){覆盖归结}
你怎么能做到这一点jQuery的/ JavaScript的?
我需要一个覆盖依据是什么网址具有在它的结束滑下。
如果(URL在末端“常见问题解答”){覆盖归结}
你怎么能做到这一点jQuery的/ JavaScript的?
如果你的URL看起来像这样http://yourdomain.com/faq
,你可以这样做:
var url = window.location.href;
var lastPart = url.substr(url.lastIndexOf('/') + 1);
if (lastPart === "faq") {
// Show your overlay
}
这将有可能检查其它结局和作用于它们。
更新:
得到它的工作,即使网址的结尾的斜线,您可以创建这样一个功能:
function getLastPart(url) {
var parts = url.split("/");
return (url.lastIndexOf('/') !== url.length - 1
? parts[parts.length - 1]
: parts[parts.length - 2]);
}
然后,您可以调用的功能等getLastPart(window.location.href)
来获取URL当前页面的最后一部分。
这里是一个工作示例,以及: http://jsfiddle.net/WuXHG/
免责声明:如果您的网址结尾,或查询字符串使用散列,你就必须首先从网址中去除,此脚本才能正常工作
您应该能够使用window.location的对象这一个正则表达式,像这样的东西:
/faq$/.test(window.location)
的新方法endsWith()
已经被添加到所述ES6规范。 对于以前的版本中,我们可以用它填充工具
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;
};
现在,你可以随便写
If (window.location.href.endsWith("faq")) {
// Show your overlay
}
参考: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith
您可以使用获得当前网址:
var currentUrl = window.location.href;
然后你可以使用的indexOf检查,如果您的令牌是字符串(这里FAQ)结束
if (currentUrl.indexOf('faq') == currentUrl.length - 3)
{
// Do something here
}