我怎么能这样做呢:

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

当前回答

这是我的代码♥

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
} 

尝试indexOf

if (foo.indexOf("franky") >= 0)
{
  ...
}

您也可以尝试搜索(正则表达式)

if (foo.search("franky") >= 0)
{
  ...
}

使用Window.location.href获取javascript格式的url。这是一个 属性,该属性将告诉您浏览器的当前URL位置。 将属性设置为其他内容将重定向页面。

if (window.location.href.indexOf('franky') > -1) {
     alert("your url contains the name franky");
}
if (window.location.href.indexOf("franky") != -1)

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

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

你可以像这样使用indexOf:

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

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

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