Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 我正在尝试在URL中搜索字符串_Jquery_Url_Window.location - Fatal编程技术网

Jquery 我正在尝试在URL中搜索字符串

Jquery 我正在尝试在URL中搜索字符串,jquery,url,window.location,Jquery,Url,Window.location,我希望使用jquery搜索某个字符串,该字符串在用户提交表单后被标记到页面url的末尾 url看起来像 https://somerandomsite.com/?page_id=1423#wpcf7-f1448-p1423-o1 字符串中唯一保持一致的部分是“wpcf7”,因此我希望通过jquery将其作为目标,如果它发现它运行一些代码。可能会以某种方式使用window.location.pathname 任何帮助都将不胜感激 ==================================

我希望使用jquery搜索某个字符串,该字符串在用户提交表单后被标记到页面url的末尾

url看起来像

https://somerandomsite.com/?page_id=1423#wpcf7-f1448-p1423-o1
字符串中唯一保持一致的部分是“wpcf7”,因此我希望通过jquery将其作为目标,如果它发现它运行一些代码。可能会以某种方式使用window.location.pathname

任何帮助都将不胜感激

============================================

我知道了,谢谢大家的帮助。我想我解释这个问题不是很清楚,所以我道歉

if(document.location.hash.substr(1,5) === 'wpcf7'){
$('#formalert').css('display', 'block');

该部分称为散列,引用为:

location.hash
这包括
#
字符本身,因此您必须跳过:

location.hash.substr(1)
要获得其背后的第一部分:

location.hash.substr(1).split('-')[0]
更新

最后的条件是:

if (document.location.hash.substr(1).split('-')[0] === 'wpcf7') {
    // do whatever
}
您可以为此使用:

str.search('wpcf7')
您可以使用:

var urlPart = location.hash.substr(1);    // Retrieve part after dash #
var pages = urlPart.split('-'); // Split this part on "-"

var myInterstingPart = page[0]; // Get the first.

没有?

可能是这样的:

if (window.location.hash.indexOf("wpcf7") != -1) {
   alert('window contains wpcf7');
}

我试过document.location.pathname.match我仍然需要在散列后找到字符串wpcf7,我该怎么做?好的,我查阅了.split()上的文档,所以我了解您现在在做什么。对于如何使用您的代码基于该字符串运行偶数,我仍然感到困惑。我已经更新了我的问题,我认为我可以如何运行它。@user1324700我明白了,你想在散列更改后立即运行代码吗?在我的回答中,我更新了这一点,我认为这变得比它需要的更复杂,在提交表单后,它只是在url的末尾附加一个id,我只想解析字符串开头的url(wpfc7)。除非您所描述的内容是必需的,否则请全部忘记哈希标记。@user1324700好吧,您说过要运行一个事件。。。那时候还不是很清楚。。。在任何情况下,只要一个
if(location.hash.substr…etc){…}
就可以了,不是吗?