コード日進月歩

しんくうの技術的な小話、メモ、つれづれ、など

URLで//(スラッシュスラッシュ)から記述してschema(プロトコル)を省略すると、現在のページのschemaで動く

いい感じにわかる情報がなかったので

出典

RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax

要約

//example.com などと http の部分(schema/プロトコル)を省略して記述した場合は、開いているページのものをそのまま使う。

内容と考え方

// で始まる net_path として定義されている。

net_path = "//" authority [ abs_path ]

また、この net_path は相対参照であることが記載されている・

The syntax for relative URI takes advantage of the <hier_part> syntax of (Section 3) in order to express a reference that is relative to the namespace of another hierarchical URI.
relativeURI = ( net_path | abs_path | rel_path ) [ "?" query ]
A relative reference beginning with two slash characters is termed a network-path reference, as defined by <net_path> in Section 3. Such references are rarely used.

これを和訳すると以下の通り

相対URIの構文は、別の階層URIの名前空間に関連する参照を表すために、(セクション3)の<hier_part>構文を利用します。
relativeURI = ( net_path | abs_path | rel_path ) [ "?" query ]
セクション3の<net_path>で定義されているように、2つのスラッシュ文字で始まる相対参照は、ネットワークパス参照と呼ばれます。このような参照はほとんど使用されません。

これらより以下のことでschema部が補完される。

  • // から始まるURIは相対参照(ネットワークパス参照)と呼ばれるの相対URI扱い
  • 相対URIなので通常の https://example.com/a.html<img src="images/sample.gif"> 記述したときに <img src="https://example.com/images/sample.gif"> と補完されるのと同じ原理でschema部が補完される。

参考サイト