2016-04-20 114 views
2

我的網頁上有一個javascript來檢查最後一個或最後一個URL是否包含字符串,但是每個條件都返回TRUE,無論我來自哪個網址。檢查前一個(-1&-2)url是否包含字符串

你能幫我解釋一下嗎?如果我從if中除去其他條件,則所有條件都變爲TRUE。

這是謝謝你的頁面。從訂購頁面(我檢查的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'); 
} 


}); 

回答

1

href是字符串類型,string.indexOf或者返回-1,如果參數沒有被發現或數字0或更高,如果是這樣,指出這是從開始的,因爲你的第二部分if語句> -2它將永遠是真實的

window.location的位置是當前位置

+0

謝謝你,我還以爲這是在以前的網址導航。你不知道我以前的網址可以檢查嗎?它看起來像這個條件不能採取URL我想檢查(從訂單頁面),所以每個條件返回false。 –

+0

[this](http://stackoverflow.com/questions/3528324/how-do-you-get-the-previous-url-in-javascript)可能會給你一些想法,但這不是一件容易的事情 –

+0

再次感謝你, 我會試試看。 –

相關問題