2

on my web I got a javascript to check if last or one before last URL contains string, but every condition return TRUE no matter which url I came from.

Can you please help me how to check it correctly? If I remove else from ifs, all conditions become TRUE.

This is thank you page. From ordering page (with urls I checking) I redirect users to /dekuji-vam (this thank you page) so because that I have to use -2 in conditions.

Thank you for your help, Michal

$(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');
}


});
Michal Běhal
  • 89
  • 1
  • 11

1 Answers1

1

href is type string, string.indexOf will either return -1 if the parameter is not found or a number 0 or higher if it is, stating that this is the position it starts from, because the second part of you if statement is > -2 it will always be true

window.location is the current location

  • Thanks, I thought it's navigation in previous urls. Don't you know how i can check before previous url? It looks like this conditions can't take URL i want to check (from order page), so every condition returns false. – Michal Běhal Apr 20 '16 at 08:59
  • [this](http://stackoverflow.com/questions/3528324/how-do-you-get-the-previous-url-in-javascript) may give you some idea's but it's not something easily done – Martin Glennon-Brown Apr 20 '16 at 09:04
  • Thank you again, I'll try it. – Michal Běhal Apr 20 '16 at 09:11