How to check if the URL contains a given string?
我怎么能这样做:
<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>
您需要添加href属性并检查indexOf而不是contains 
<script type="text/javascript">
$(document).ready(function () {
    if(window.location.href.indexOf("franky") > -1) {
       alert("your url contains the name franky");
    }
});
</script>
if (window.location.href.indexOf("franky") != -1)
would do it. Alternatively, you could use a regexp:
if (/franky/.test(window.location.href))
像这样:
    <script type="text/javascript">
        $(document).ready(function () {
            if(window.location.href.indexOf("cart") > -1) 
            {
                 alert("your url contains the name franky");
            }
        });
    </script>
下一篇: 如何检查URL是否包含给定的字符串?
