首页 > 编程语言 >JavaScript Window Location

JavaScript Window Location

时间:2022-08-28 11:56:17浏览次数:72  
标签:location JavaScript window js Window Location 页面

JavaScript Window Location  https://www.w3school.com.cn/js/js_window_location.asp

window.location 对象可用于获取当前页面地址(URL)并把浏览器重定向到新页面。

Window Location

window.location 对象可不带 window 前缀书写。

一些例子:

  • window.location.href 返回当前页面的 href (URL)
  • window.location.hostname 返回 web 主机的域名
  • window.location.pathname 返回当前页面的路径或文件名
  • window.location.protocol 返回使用的 web 协议(http: 或 https:)
  • window.location.assign 加载新文档

Window Location Href

window.location.href 属性返回当前页面的 URL。

实例

显示当前页面的 href (URL):

document.getElementById("demo").innerHTML = "页面位置是 " + window.location.href;

结果是:

页面位置是 http://www.w3school.com.cn/js/js_window_location.asp

亲自试一试

Window Location 主机名

window.location.hostname 属性返回(当前页面的)因特网主机的名称。

实例

显示主机的名称:

document.getElementById("demo").innerHTML = "页面主机名是 " + window.location.hostname;

结果是:

页面主机名是 www.w3school.com.cn

亲自试一试

Window Location 路径名

window.location.pathname 属性返回当前页面的路径名。

实例

显示当前 URL 的路径名:

document.getElementById("demo").innerHTML = "页面路径是 " + window.location.pathname;

结果是:

页面路径是 /js/js_window_location.asp

亲自试一试

Window Location 协议

window.location.protocol 属性返回页面的 web 协议。

实例

显示 web 协议:

document.getElementById("demo").innerHTML = "页面协议是 " + window.location.protocol;

结果是:

页面协议是 http:

亲自试一试

Window Location 端口

window.location.port 属性返回(当前页面的)互联网主机端口的编号。

实例

显示主机的端口号:

document.getElementById("demo").innerHTML = "端口号是: " + window.location.port;

亲自试一试

大多数浏览器不会显示默认端口号(http 为 80,https 为 443)。

Window Location Assign

window.location.assign() 方法加载新文档。

实例

加载新文档:

<html>
<head>
<script>
function newDoc() {
    window.location.assign("https://www.w3school.com.cn")
 }
</script>
</head>
<body>

<input type="button" value="Load new document" onclick="newDoc()">

</body>
</html> 

标签:location,JavaScript,window,js,Window,Location,页面
From: https://www.cnblogs.com/jmbt/p/16632501.html

相关文章

  • 端口占用解决(Window系统)——结束占用端口的进程
    1.打开dos窗口。2.查看端口的占用情况。输入 netstat-ano查看所有端口的占用情况。记住PID(进程号)。 如果觉得太长了,也可以依据端口号输入 netstat-aon|findstr"......
  • javascript:void(0) 含义
    javascript:void(0)含义|菜鸟教程 https://www.runoob.com/js/js-void.html我们经常会使用到 javascript:void(0) 这样的代码,那么在JavaScript中 javascript:vo......
  • JavaScript 正则表达式
    JavaScript正则表达式|菜鸟教程 https://www.runoob.com/js/js-regexp.html正则表达式(英语:RegularExpression,在代码中常简写为regex、regexp或RE)使用单个字符串来描......
  • PowerShell教程 - WMI操作(Interact With Windows Management Instrumentation)
    更新记录转载请注明出处。2022年8月28日发布。2022年8月28日从笔记迁移到博客。WMI操作(InteractWithWindowsManagementInstrumentation)说明WindowsManageme......
  • JavaScript实现栈结构(Stack)
    Js实现栈结构一、前言1.1什么是数据结构数据结构就是在计算机中,存储和组织数据的方式。例如:图书管理,怎样摆放图书才能既能放很多书,也方便取?常见的数据结构:栈(Stack)......
  • 5 - Windows端Git可视化工具TortoiseGit(小乌龟)
     TortoiseGit是一个开放的Git版本控制系统的源客户端,只运行于Windows系统中,与操作系统紧密结合,使用起来非常方便 一、TortoiseGit的下载安装1、TortoiseGit软件链接:h......
  • Javascript中逗号和分号区别
     因为分号被视为多个语句,逗号被视为一个语句。那么多个语句和一个语句有什么区别呢?各种需要大括号的地方可以省略大括号,从而减少字节数比如 if(condition){foo(......
  • how to make the windows console work with utf-8 encoded project
    theconsoleofthewindowsosisnotworkingintheutf-8encoding,bydefault.Whenyouforceyourcodebeencodedwithutf-8,theconsolewillnotprintwhat......
  • JavaScript Window Location
    JavaScriptWindowLocation https://www.w3school.com.cn/js/js_window_location.aspwindow.location对象可用于获取当前页面地址(URL)并把浏览器重定向到新页面。Win......
  • javascript脚本应该写在代码的什么地方
    javascript脚本应该写在代码的什么地方_百度知道 https://zhidao.baidu.com/question/981893155394447739.htmlcmvibahy2013-10-10 · TA获得超过4116个赞关注 ......