gpt4 book ai didi

javascript - 检查前一个 (-1 & -2) url 是否包含字符串

转载 作者:行者123 更新时间:2023-11-30 00:10:23 26 4
gpt4 key购买 nike

在我的网站上,我有一个 javascript 来检查最后一个或最后一个 URL 之前是否包含字符串,但无论我来自哪个 url,每个条件都返回 TRUE。

你能帮我看看如何正确检查吗?如果我从 ifs 中删除 else,所有条件都变为真。

这是感谢页面。从订购页面(我检查了 url),我将用户重定向到/dekuji-vam(这个感谢页面),因为我必须在条件中使用 -2。

谢谢你的帮助,迈克尔

$(document).ready(function () {

if((window.location.href.indexOf("kurz-chytre-seo") > -1) || (window.location.href.indexOf("kurz-chytre-seo") > -2)) {
window.history.pushState('dekuji-vam', 'Děkuji Vám', 'http://www.pavelungr.cz/skoleni/kurz-chytre-seo/dekuji-vam');
$('.dc1').css('display', 'block');
}

else if((window.location.href.indexOf("vam-workshop-analyzy-klicovych-slov") > -1) || (window.location.href.indexOf("workshop-analyzy-klicovych-slov") > -2)) {
window.history.pushState('dekuji-vam', 'Děkuji Vám', 'http://www.pavelungr.cz/skoleni/workshop-analyzy-klicovych-slov/dekuji-vam');
$('.dc2').css('display', 'block');
}

else if((window.location.href.indexOf("workshop-seo-pro-wordpress") > -1) || (window.location.href.indexOf("workshop-seo-pro-wordpress") > -2)) {
window.history.pushState('dekuji-vam', 'Děkuji Vám', 'http://www.pavelungr.cz/skoleni/workshop-seo-pro-wordpress/dekuji-vam');
$('.dc3').css('display', 'block');
}

else if((window.location.href.indexOf("interni-skoleni-pro-firmy") > -1) || (window.location.href.indexOf("interni-skoleni-pro-firmy") > -2)) {
window.history.pushState('dekuji-vam', 'Děkuji Vám', 'http://www.pavelungr.cz/skoleni/interni-skoleni-pro-firmy/dekuji-vam');
$('.dc4').css('display', 'block');
}

else if((window.location.href.indexOf("seo-pro-copywritery") > -1) || (window.location.href.indexOf("seo-pro-copywritery") > -2)) {
window.history.pushState('dekuji-vam', 'Děkuji Vám', 'http://www.pavelungr.cz/skoleni/seo-pro-copywritery/dekuji-vam');
$('.dc5').css('display', 'block');
}

else if((window.location.href.indexOf("workshop-na-strukturovana-data-rich-snippets-mikroformaty-mikrodata") > -1) || (window.location.href.indexOf("workshop-na-strukturovana-data-rich-snippets-mikroformaty-mikrodata") > -2)) {
window.history.pushState('dekuji-vam', 'Děkuji Vám', 'http://www.pavelungr.cz/skoleni/workshop-na-strukturovana-data-rich-snippets-mikroformaty-mikrodata/dekuji-vam');
$('.dc6').css('display', 'block');
}

else if((window.location.href.indexOf("jak-spravne-udelat-analyzu-klicovych-slov-tvorba-analyzy") > -1) || (window.location.href.indexOf("jak-spravne-udelat-analyzu-klicovych-slov-tvorba-analyzy") > -2)) {
window.history.pushState('dekuji-vam', 'Děkuji Vám', 'http://www.pavelungr.cz/skoleni/jak-spravne-udelat-analyzu-klicovych-slov-tvorba-analyzy/dekuji-vam');
$('.dc7').css('display', 'block');
}

else if((window.location.href.indexOf("jak-spravne-implementovat-analyzu-klicovych-slov-uprava-webu-a-strategie") > -1) || (window.location.href.indexOf("jak-spravne-implementovat-analyzu-klicovych-slov-uprava-webu-a-strategie") > -2)) {
window.history.pushState('dekuji-vam', 'Děkuji Vám', 'http://www.pavelungr.cz/skoleni/jak-spravne-implementovat-analyzu-klicovych-slov-uprava-webu-a-strategie/dekuji-vam');
$('.dc8').css('display', 'block');
}


});

最佳答案

href 是字符串类型,string.indexOf如果找不到参数,将返回 -1,如果找到,则返回 0 或更高的数字,说明这是它开始的位置,因为你的第二部分 if 语句是 > -2 它将永远是 true

window.location 是当前位置

关于javascript - 检查前一个 (-1 & -2) url 是否包含字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36737394/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com