如何检查URL末尾是否有特定的字符串 [英] How to check if URL has a specific string at the end

查看:70
本文介绍了如何检查URL末尾是否有特定的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据网址末尾的内容向下滑动一个叠加层。

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

if(URL末尾有'faq')
{
overlay to down
}

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

你怎么能在jQuery / JavaScript中做到这一点?

How can you do that in jQuery/JavaScript?

推荐答案

如果你的网址看起来像这样 http://yourdomain.com/faq ,你可以做这样的事情:

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.

更新:

为了让它工作,即使URL有一个斜杠,你也可以创建一个这样的函数:

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]);
}

然后您可以像 getLastPart(窗口)一样调用该函数.location.href)获取当前页面的URL的最后一部分。

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

以下是一个工作示例: http://jsfiddle.net/WuXHG/

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

免责声明:如果您的网址最后使用哈希值或查询字符串,则必须先从网址中删除该网址,以使此脚本正常运行

这篇关于如何检查URL末尾是否有特定的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆