码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript获取当前url路径

时间:2018-01-15 11:10:44      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:col   div   文章   cal   host   c#   arch   html   script   

1、假设当前页完整地址是:http://localhost:61768/Home/Index?id=2

//获取当前窗口的Url
var url = window.location.href;
//结果:http://localhost:61768/Home/Index?id=2

//获取当前窗口的主机名
var host = window.location.host;
//结果:localhost:61768

//获取当前窗口的端口
var port = window.location.port;
//结果:61768

//获取当前窗口的路径
var pathname = window.location.pathname;
//结果:/Home/Index

//获取当前文档的Url
var URL = document.URL;
//结果:http://localhost:61768/Home/Index?id=2

//获取参数
var search = window.location.search;
//结果:?id=2

2、跳出当前窗口

//跳出当前窗口,打开新窗口
window.open("http://www.baidu.com");

3、document与window

document默示的是一个文档对象,window默示的是一个窗口对象,一个窗口下可以有多个文档对象。

所以一个窗口下只有一个window.location.href,可能有多个document.URL、document.location.href

window.location.href 和 document.location.href 可以被赋值,然后跳转到其它页面,document.URL只能读不能赋值。


 相关文章:C# Request 获取Url

JavaScript获取当前url路径

标签:col   div   文章   cal   host   c#   arch   html   script   

原文地址:https://www.cnblogs.com/cang12138/p/7677239.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!