LoginSignup
2
2

More than 5 years have passed since last update.

URIを解析するJavaScriptのfunction

Last updated at Posted at 2017-02-06

Aタグを利用して、URIの構成要素を解析する

Aタグのhref要素を利用して、簡易的にURIを解析する。

/**
 * URIを解析する。
 * @param uri URI
 * @returns {{protocol: (*|null|string|number), hostname: (*|string), port: (*|string|number|Function), path: (*|string), query: (*|string)}}
 */
function parseUri(uri) {
    var parser = document.createElement('a');
    parser.href = uri;

    return {
        protocol: parser.protocol,
        hostname: parser.hostname,
        port: parser.port,
        path: parser.pathname,
        query: parser.search
    };
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2