2013-10-19 60 views

回答

12

使用的indexOf - 它將測試適用於所有的路徑名開始/about/

if (document.location.pathname.indexOf("/about/") == 0) { 
    //Code goes here 
} 
2
if (document.location.pathname.indexOf("/about/") === 0) { 
     //Code goes here 
    } 

這將檢查以確保pathname總是以該字符串開始。如果您有興趣更具體地檢查格式,則需要使用regex