首页/javascript外包/window.location.href 如何用js得到当前页面的url信息方法(JS获取当前页面的URL)
window.location.href 如何用js得到当前页面的url信息方法(JS获取当前页面的URL)
发布-xiaoming | 浏览量-
如何用js得到当前页面的url信息方法(JS获取当前页面的URL)
1,设置或获取对象指定的文件名或路径。
alert(window.location.pathname)
2,设置或获取整个 URL 为字符串。
alert(window.location.href);
3,设置或获取与 URL 关联的端口号码。
alert(window.location.port)
4,设置或获取 URL 的协议部分。
alert(window.location.protocol)
5,设置或获取 href 属性中在井号“#”后面的分段。
alert(window.location.hash)
6,设置或获取 location 或 URL 的 hostname 和 port 号码。
alert(window.location.host)
7,设置或获取 href 属性中跟在问号后面的部分。
alert(window.location.search)
8,获取变量的值(截取等号后面的部分)
var url = window.location.search;
// alert(url.length);
// alert(url.lastIndexOf('='));
var loc = url.substring(url.lastIndexOf('=')+1, url.length);
9,用来得到当前网页的域名
var domain = document.domain;
JS获取当前页面的URL
var url;
url = window.location.href; /* 获取完整URL */
alert(url); /* http://127.0.0.1:8020/Test/index.html#test?name=test */
url = window.location.pathname; /* 获取文件路径(文件地址) */
alert(url); /* /Test/index.html */
url = window.location.protocol; /* 获取协议 */
alert(url); /* http */
url = window.location.host; /* 获取主机地址和端口号 */
alert(url); /* http://127.0.0.1:8020/ */
url = window.location.hostname; /* 获取主机地址 */
alert(url); /* http://127.0.0.1/ */
url = window.location.port; /* 获取端口号 */
alert(url); /* 8020 */
url = window.location.hash; /* 获取锚点(“#”后面的分段) */
alert(url); /* #test?name=test */
url = window.location.search; /* 获取属性(“?”后面的分段) */
alert(url);
/* 如果需要URL中的某一部分,可以自己进行处理 */
url = window.location.pathname;
url = url.substring(url.lastIndexOf('/') + 1, url.length);
alert(url); /* /index.html */
/*
* 如果页面使用了框架(frameset)
* 要获取到指定页面的URL
* 只要把window换成指定的页面即可
*/
/* 'frame'为指定页面的class名 */
var url = window.parent.frames['frame'].location.href;
/* 获取当前地址栏中显示的URL */
var url = window.parent.location.href;
/* window parent 可互换 */
var url = parent.window.location.href;
原文地址:http://www.35ui.cn/post/20170417768.html 标签:javascriptjsurllocationWindow
上一条: jquery.cookie() 方法的使用(读取、写入、删除)
下一条: Shell 显示带颜色字体 shell echo 字体颜色
或许你还对下面的文章感兴趣