javascript - 使用 JQuery 检查 url 是否包含字符串

标签 javascript jquery url

我有一个包含选择选项的页面,我正在使用 JQuery 刷新页面并在单击选项时向 url 添加字符串。现在我需要一种方法来检查浏览器 URL 以查看它是否包含所述字符串。

查看其他线程,我认为 indexOf会起作用,但是当尝试这个时它不起作用。我还能如何检查 URL 是否包含类似 ?added-to-cart=555 的内容?完整的 URL 通常如下所示:http://my-site.com ,单击其中一个选项后,页面重新加载后看起来像这样:http://my-site.com/?added-to-cart=555 。我只需要检查 URL 是否包含 ?added-to-cart=555一点。

这是我所拥有的:

jQuery("#landing-select option").click(function(){

 $('#product-form').submit();

    window.location.href += $(this).val()

});

jQuery(document).ready(function($) {
if(window.location.indexOf("?added-to-cart=555") >= 0)
            {
                 alert("found it");
            }
});

最佳答案

Use Window.location.href to take the url in javascript. it's a property that will tell you the current URL location of the browser. Setting the property to something different will redirect the page.

if (window.location.href.indexOf("?added-to-cart=555") > -1) {
    alert("found it");
}

关于javascript - 使用 JQuery 检查 url 是否包含字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21718282/

相关文章:

javascript - 使用 jQuery 滚动 div

javascript - Chrome 中的 JQuery scrollTop 动画断断续续

javascript - JavaScript 的客户端 DOM 打开重定向

javascript - 如何有效地使用 jQuery 来乘以动态呈现表的列

javascript - 在 angularjs 中获取并加载图像

javascript - 拓宽职能范围

jquery - 使用 JQuery 重新加载时将填充设置为元素

.htaccess - Joomla 搜索返回无效的网络链接 URL(SEO 友好 URL 不起作用)

ruby-on-rails - Rails 3 UTF-8 查询字符串出现在 URL 中?

javascript - 从 Telerik 切换到 Kendo UI 缺少​​ orderBy 和 filter By