如何检查URL是否包含给定的字符串?

Ray*_*ess 343 javascript url jquery

我怎么能做这样的事情:

<script type="text/javascript">
$(document).ready(function () {
    if(window.location.contains("franky")) // This doesn't work, any suggestions?
    {
         alert("your url contains the name franky");
    }
});
</script>
Run Code Online (Sandbox Code Playgroud)

J.W*_*.W. 620

您需要添加href属性并检查indexOf而不是contains

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
  $(document).ready(function() {
    if (window.location.href.indexOf("franky") > -1) {
      alert("your url contains the name franky");
    }
  });
</script>
Run Code Online (Sandbox Code Playgroud)

  • @Vennsoh你无法找到"买车",因为你需要查看`window.location.hash`而不是'window.location.href`.只需在OP函数中交换这些值即可. (6认同)
  • @Elshan 因为,严格来说,`.href.indexOf("franky")` [可以返回值 0](https://www.w3schools.com/jsref/jsref_indexof.asp) 如果`.href` “坦率”。当然,在这种情况下,它永远不会这样做,因为`.href` 总是以协议开头,通常是“http:”或“file:”。尽管如此,在与 [`indexOf()`](https://www.w3schools.com/jsref/ jsref_indexof.asp)。 (4认同)
  • 我有这样一个很长的网址,http://preview.tbwabox.co.nz/_v005/index.html#buying-a-car,我想检查这个字符串是否有"买车但脚本"不工作? (3认同)
  • @JW 为什么`&gt;-1` 不能使用`&gt;0`? (2认同)

Nic*_*itz 99

if (window.location.href.indexOf("franky") != -1)
Run Code Online (Sandbox Code Playgroud)

会做的.或者,您可以使用正则表达式:

if (/franky/.test(window.location.href))
Run Code Online (Sandbox Code Playgroud)

  • +1`if(/search/.test(self.location.href))` (16认同)
  • 两个选项之间的优缺点将是对这个答案的一个很好的补充。 (5认同)

Sar*_*raz 25

你会这样使用indexOf:

if(window.location.href.indexOf("franky") != -1){....}
Run Code Online (Sandbox Code Playgroud)

另请注意添加href字符串否则您将执行:

if(window.location.toString().indexOf("franky") != -1){....}
Run Code Online (Sandbox Code Playgroud)


Adr*_*les 23

像这样:

    <script type="text/javascript">
        $(document).ready(function () {
            if(window.location.href.indexOf("cart") > -1) 
            {
                 alert("your url contains the name franky");
            }
        });
    </script>
Run Code Online (Sandbox Code Playgroud)


Ali*_*aru 13

window.location不是String,但它有一个toString()方法.所以你可以这样做:

(''+window.location).includes("franky")
Run Code Online (Sandbox Code Playgroud)

要么

window.location.toString().includes("franky")
Run Code Online (Sandbox Code Playgroud)

来自旧的Mozilla文档:

位置对象具有返回当前URL的toString方法.您还可以为window.location分配一个字符串.这意味着您可以使用window.location,就像在大多数情况下它是一个字符串一样.有时,例如,当您需要在其上调用String方法时,您必须显式调用toString.

  • 在Firefox 48中,String.prototype.contains()已被删除.仅使用String.prototype.includes().[见这里](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (2认同)

Ste*_*ter 8

正则表达方式:

var matches = !!location.href.match(/franky/); //a boolean value now
Run Code Online (Sandbox Code Playgroud)

或者在一个简单的声明中,您可以使用:

if (location.href.match(/franky/)) {
Run Code Online (Sandbox Code Playgroud)

我用它来测试网站是在本地运行还是在服务器上运行:

location.href.match(/(192.168|localhost).*:1337/)
Run Code Online (Sandbox Code Playgroud)

这将检查href是否包含其中一个,192.168或者localhost后面是AND :1337.

正如您所看到的,当条件变得有点棘手时,使用正则表达式优于其他解决方案.


小智 6

document.URL应该得到你URL

if(document.URL.indexOf("searchtext") != -1) {
    //found
} else {
    //nope
} 
Run Code Online (Sandbox Code Playgroud)


six*_*pro 6

试试这个,它更短,完全如下window.location.href:

if (document.URL.indexOf("franky") > -1) { ... }
Run Code Online (Sandbox Code Playgroud)

如果你想检查以前的网址:

if (document.referrer.indexOf("franky") > -1) { ... }
Run Code Online (Sandbox Code Playgroud)

  • 如果你要投票,至少要说明原因 (2认同)

小智 5

更容易得到

<script type="text/javascript">
$(document).ready(function () {
    var url = window.location.href;
    if(url.includes('franky'))    //includes() method determines whether a string contains specified string.
    {
         alert("url contains franky");
    }
});
</script>
Run Code Online (Sandbox Code Playgroud)


小智 5

如果将字符串转换为小写或大写,这将是一个很好的做法,因为 indexof() 方法区分大小写。

如果您的搜索不区分大小写,您可以简单地使用 indexOf() 方法,而无需将原始字符串转换为小写或大写:

var string= location.href;
var convertedString= string.toLowerCase();

if(convertedString.indexOf('franky') != -1)
{
    alert("url has franky");
}
else
{
    alert("url has no franky");
}
Run Code Online (Sandbox Code Playgroud)


小智 5

相反,我喜欢这种方法。

top.location.pathname.includes('franky')
Run Code Online (Sandbox Code Playgroud)

它在很多情况下都有效。