2014-10-23 3 views
1

필요한 URL을 얻을 수 있도록 JavaScript에서 URL을 구문 분석하는 방법이 있는지 궁금합니다. 예를 들어, 아래의 두 URL은 처음에 숫자를 얻으 려한다면 무엇을 사용할 수 있습니까? 변수에 21을 저장하기 위해 두 번째 URL을 사용하면 동일할까요? 감사.JavaScript의 구문 분석 URL

http://domain.com/test/?=23

http://domain.com/test/21

+0

에 https : //developer.mozilla를 .org/ko-US/docs/Web/API/Location – mplungjan

+0

두 번째 형식의 경우 :'num = (ua = url.split ("/")) [ua.length-1]' – Duncan

답변

4
var parser = document.createElement('a'); 
parser.href = "http://example.com:3000/pathname/?search=test#hash"; 

parser.protocol; // => "http:" 
parser.host;  // => "example.com:3000" 
parser.hostname; // => "example.com" 
parser.port;  // => "3000" 
parser.pathname; // => "/pathname/" 
parser.hash;  // => "#hash" 
parser.search; // => "?search=test" 

PHPJS 당신이 원하는 것을하는 당신에게 PHP parse_url 기능의 번역을 제공 URI Parsing with Javascript

0

:

parse_url("http://domain.com/test/?=23"); 
Object {query: "=23", path: "/test/", host: "domain.com", scheme: "http"} 

parse_url("http://domain.com/test/21"); 
Object {path: "/test/21", host: "domain.com", scheme: "http"}