6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

URLの一部を組み替える

Last updated at Posted at 2015-08-05

URL クラスを使うと、以下のように URL をパーツに分解するのが可能ですが:

> new URL('http://example.com/foo/bar?x=1#hash')
{
  hash: "#hash"
  host: "example.com"
  hostname: "example.com"
  href: "http://example.com/foo/bar?x=1#hash"
  origin: "http://example.com"
  password: ""
  pathname: "/foo/bar"
  port: ""
  protocol: "http:"
  search: "?x=1"
  username: ""
}

もし、その一部を書き換えて再構築したい場合はこうする:

> var urlObj = new URL('http://example.com/foo/bar?x=1#hash')
> urlObj.hostname = 'qiita.com'
> urlObj.pathname = '/unk'
> urlObj.hash = 'cnk'
> urlObj.toString()
"http://qiita.com/unk?x=1#cnk"
6
6
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
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?