복붙노트

[JQUERY] 자바 스크립트 - URL 경로의 부분을 얻기

JQUERY

자바 스크립트 - URL 경로의 부분을 얻기

해결법


  1. 1.현재 윈도우가 내장되어있는 스크립팅 window.location 객체의 속성이 있습니다.

    현재 윈도우가 내장되어있는 스크립팅 window.location 객체의 속성이 있습니다.

    // If URL is http://www.somedomain.com/account/search?filter=a#top
    
    window.location.pathname // /account/search
    
    // For reference:
    
    window.location.host     // www.somedomain.com (includes port if there is one)
    window.location.hostname // www.somedomain.com
    window.location.hash     // #top
    window.location.href     // http://www.somedomain.com/account/search?filter=a#top
    window.location.port     // (empty string)
    window.location.protocol // http:
    window.location.search   // ?filter=a  
    

  2. 2.

    window.location.href.split('/');
    

    당신이 일반 배열처럼 액세스 할 수있는 모든 URL의 부분을 포함하는 배열을 줄 것이다.

    아니면 점점 더 우아한 솔루션은 경로 부품, @Dylan에 의해 제안 :

    window.location.pathname.split('/');
    

  3. 3.이 경우 현재의 URL를 사용 window.location.pathname 그렇지 않으면이 정규 표현식을 사용합니다 :

    이 경우 현재의 URL를 사용 window.location.pathname 그렇지 않으면이 정규 표현식을 사용합니다 :

    var reg = /.+?\:\/\/.+?(\/.+?)(?:#|\?|$)/;
    var pathname = reg.exec( 'http://www.somedomain.com/account/search?filter=a#top' )[1];
    

  4. 4.URL이라는 유용한 웹 API 방법이있다

    URL이라는 유용한 웹 API 방법이있다

    const를 URL = 새 URL ( 'http://www.somedomain.com/account/search?filter=a#top'); CONSOLE.LOG (url.pathname.split ( '/')); CONST PARAMS = 새로운 URLSearchParams (url.search) CONSOLE.LOG (params.get ( "필터"))


  5. 5.당신은 사람 (현재에서는 window.location에서) 추상적 인 URL 문자열이있는 경우,이 트릭을 사용할 수 있습니다 :

    당신은 사람 (현재에서는 window.location에서) 추상적 인 URL 문자열이있는 경우,이 트릭을 사용할 수 있습니다 :

    let yourUrlString = "http://example.com:3000/pathname/?search=test#hash";
    
    let parser = document.createElement('a');
    parser.href = yourUrlString;
    
    parser.protocol; // => "http:"
    parser.hostname; // => "example.com"
    parser.port;     // => "3000"
    parser.pathname; // => "/pathname/"
    parser.search;   // => "?search=test"
    parser.hash;     // => "#hash"
    parser.host;     // => "example.com:3000"
    

    jlong를 덕분에


  6. 6.경우에 당신은 당신이 변수에 저장되어 있다는 URL의 일부를 얻으려면, 내가 URL-구문 분석을 추천 할 수 있습니다

    경우에 당신은 당신이 변수에 저장되어 있다는 URL의 일부를 얻으려면, 내가 URL-구문 분석을 추천 할 수 있습니다

    const Url = require('url-parse');
    const url = new Url('https://github.com/foo/bar');
    

    문서에 따르면, 다음과 같은 부분을 추출 :

  7. from https://stackoverflow.com/questions/6944744/javascript-get-portion-of-url-path by cc-by-sa and MIT license