2015-07-10 43 views
0

我正在尋找隱藏系統中的某些項目,我無法控制HTML。jquery如果URL中的任何內容在特定頁面後

我想要在/ jobs /頁面上的url後面有任何內容。因此,對於/作業/我的功能不會觸發,一旦它變成/ jobs/XXXX,那麼該功能就會觸發。

我已經試過,但我不知道它實際上是在做什麼,因爲它證實了兩個/就業/和/職位/ XXX

if(window.location.href.indexOf("jobs") != -1) { 
    alert("your url contains the word jobs"); 
} 

回答

2

您可以使用正則表達式來檢測,如果它明確結束在/jobs/$

if((new RegExp('\/jobs\/$')).test(window.location.href)){ 
} 

原因

if(window.location.href.indexOf("jobs") != -1) { 
    alert("your url contains the word jobs"); 
} 

是工作,無論它在/jobs/結束與否是因爲indexOf()檢查的作業存在任何地方字符串

0

我假設你只有在事實感興趣,即使網址以「工作/」或沒有結束。這是你可以做什麼來檢查它是否以'jobs /'結束:

var str = window.location.href; 

if (typeof String.prototype.endsWith !== 'function') { 
    String.prototype.endsWith = function(suffix) { 
     return this.indexOf(suffix, this.length - suffix.length) !== -1; 
    }; 
} 

if(str.endsWith('jobs/')) 
    console.log("Yes!"); //Do things here 
else 
    console.log("Nope"); // or here.. 
相關問題