Window location.href 属性
- Forrige side hostname
- Næste side origin
- Gå tilbage et niveau Window Location
定义和用法
location.href
属性设置或返回当前页面的整个 URL。
实例
例子 1
获取当前页面的 URL:
let url = location.href;
例子 2
设置当前页面的 URL:
location.href = "";
例子 3
将 href 值设置为指向页面内的锚点:
location.href = "#top";
例子 4
将 href 值设置为指向电子邮件地址(将打开软件并创建新电子邮件):
location.href = "mailto:someone@example.com";
语法
返回 href 属性:
location.href
设置 href 属性:
location.href = URL
属性值
值 | 描述 |
---|---|
URL |
绝对 URL,例如: http://www.example.com/index.html 相对 URL,例如: index.html 锚 URL,例如: location.href="#top" 新协议,例如: ftp://someftpserver.com mailto:someone@example.com file://host/path/example.txt |
返回值
类型 | 描述 |
---|---|
字符串 | 完整 URL 的页面,包括协议(如 https://)。 |
Browserstøtte
Alle browsere understøtter location.href
:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Støtte | Støtte | Støtte | Støtte | Støtte | Støtte |
- Forrige side hostname
- Næste side origin
- Gå tilbage et niveau Window Location