Window location.protocol

实例

返回当前 URL 的协议:

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <h2>protocol 属性</h2>
  5. <p id="demo">
  6. <script>
  7. let protocol = location.protocol;
  8. document.getElementById("demo").innerHTML = protocol;
  9. </script>
  10. </body>
  11. </html>

定义与用法

protocol 属性设置或返回当前 URL 的协议,包括冒号(:)。

protocol 是一种规定计算机之间数据传输方式的标准。


语法

返回 protocol 属性:

  1. location.protocol

设置 protocol 属性:

  1. location.protocol = protocol

Property Value

参数描述
protocolURL 的协议。 比如:
  • file:
  • ftp:
  • http:
  • https:
  • mailto:

返回值

类型描述
A string当前 URL 的协议,包括冒号(:)

浏览器支持

location.protocol 所有浏览器都支持:

属性
location.protocolYesYesYesYesYes

分类导航