我怎么能这样做呢:

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

当前回答

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

会这么做的。或者,你可以使用regexp:

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

其他回答

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

会这么做的。或者,你可以使用regexp:

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

试试这个,它更短,工作完全像window.location.href:

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

如果你想检查之前的URL:

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

如果将字符串转换为小写或大写,这将是一个很好的实践,因为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");
}

这是我的代码♥

function CheckUrl(url) {
var checkA = url.match(/(https|http):\/\/(.*?)\.(.*?)\.(.*?)(\/|'')/g);
var checkP = url.match(/(https|http):\/\/(.*?)\.(.*?)(\/|'')/g);
if (checkA != null || checkP != null) {
    return true;
}
else {
    console.log("Error", "The link is not valid");
}
return false;}

文档。URL应该给你URL和

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