检查url是否包含带有JQuery的字符串 [英] Check if url contains string with JQuery

查看:173
本文介绍了检查url是否包含带有JQuery的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a page with select options and I am using JQuery to refresh the page and add a string to the url when an option is clicked. Now I need a way to check the browsers url to see if it contains said string.

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

Looking on other threads I thought indexOf would work, but when trying this it doesn't work. How else would I check if the URL contains something like ?added-to-cart=555? The complete URL would normally look like: http://my-site.com, and after clicking one of the options it looks like this after page reload: http://my-site.com/?added-to-cart=555. I just need to check to see if the URL contains that ?added-to-cart=555 bit.

以下是我的内容:

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");
            }
});


推荐答案


使用Window.location .href在javascript中获取url。它是
属性,它会告诉您浏览器的当前URL位置。
将属性设置为不同的属性将重定向页面。

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");
}

这篇关于检查url是否包含带有JQuery的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆