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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4597050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:09:58  来源:igfitidea点击:

How to check if the URL contains a given string?

javascriptjqueryurl

提问by RayLoveless

How could I do something like this:

我怎么能做这样的事情:

<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>

回答by J.W.

You need add href property and check indexOfinstead of contains

您需要添加 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>

回答by NickFitz

if (window.location.href.indexOf("franky") != -1)

would do it. Alternatively, you could use a regexp:

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

if (/franky/.test(window.location.href))

回答by Sarfraz

You would use indexOflike this:

你会indexOf像这样使用:

if(window.location.href.indexOf("franky") != -1){....}

Also notice the addition of hreffor the string otherwise you would do:

还要注意href字符串的添加,否则你会这样做:

if(window.location.toString().indexOf("franky") != -1){....}

回答by Adrian Gonzales

like so:

像这样:

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

回答by Alin Purcaru

window.locationisn't a String, but it has a toString()method. So you can do it like this:

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

(''+window.location).includes("franky")

or

或者

window.location.toString().includes("franky")

From the old Mozilla docs:

来自旧的 Mozilla 文档

Location objects have a toString method returning the current URL. You can also assign a string to window.location. This means that you can work with window.location as if it were a string in most cases. Sometimes, for example when you need to call a String method on it, you have to explicitly call toString.

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

回答by Stephan Bijzitter

The regex way:

正则表达式方式:

var matches = !!location.href.match(/franky/); //a boolean value now

Or in a simple statement you could use:

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

if (location.href.match(/franky/)) {

I use this to test whether the website is running locally or on a server:

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

location.href.match(/(192.168|localhost).*:1337/)

This checks whether the hrefcontains either 192.168or localhostAND is followed by :1337.

这将检查href是否包含192.168localhostAND 后跟:1337

As you can see, using regex has its advantages over the other solutions when the condition gets a bit trickier.

如您所见,当条件变得有点棘手时,使用正则表达式比其他解决方案更具优势。

回答by Suman

document.URLshould get you the URLand

document.URL应该让你URL

if(document.URL.indexOf("searchtext") != -1) {
    //found
} else {
    //nope
} 

回答by sixstarpro

Try this, it's shorter and works exactly as window.location.href:

试试这个,它更短,工作原理完全相同window.location.href

if (document.URL.indexOf("franky") > -1) { ... }

also if you want to check the previous URL:

如果您想检查以前的 URL:

if (document.referrer.indexOf("franky") > -1) { ... }

回答by Vishnu Dev

Easier it gets

更容易获得

<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>

回答by Chandu

Try this:

尝试这个:

<script type="text/javascript">             
    $(document).ready
    (
        function () 
        { 
            var regExp = /franky/g;
            var testString = "something.com/frankyssssddsdfjsdflk?franky";//Inyour case it would be window.location;
            if(regExp.test(testString)) // This doesn't work, any suggestions.                 
            {                      
                alert("your url contains the name franky");                 
            }             
        }
    );         
</script>