2014-04-01 58 views
2

他们无论如何都要在javascript中复制这样的PHP函数?

$uri = explode("/",substr($_SERVER['REQUEST_URI'],1)); 
if((isset($uri[0])) && ($uri[0]!="")) {$page = $uri[0];} else {$page = "home";} 
+0

请参阅http://www.w3schools.com/jsref/jsref_substr.asp和http://stackoverflow.com/questions/12241138/what-is-the-script-in-javascript-equals-to-serverrequest- uri-in-php –

+0

@MarcB - 你介意给我举个例子吗? – Curtis

回答

5

试试这个:

var uri = location.pathname.substr(1).split('/'); 
if(0 in uri && uri[0] != "") { 
    var page = uri[0]; 
} else { 
    var page = "home"; 
} 

而一个JSFiddle以行动证明这一点。

+0

这会给你“https:”,为什么不使用'location.pathname'? –

+0

更新为使用路径名,substr删除前导'/' – Styphon

相关问题