js获取页面url参数等数据
dearweb
发布:2021-05-05 23:09:57阅读:
我们在进行web工程的过程中,经常需要获取页面地址以及参数的问题,今天我与大家分享一下如果获取页面地址以及参数。
<script type="text/javascript">
url = window.location.href; /* 获取完整URL */
alert(url); /* http://127.0.0.1:8080/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:8080/ */
url = window.location.hostname; /* 获取主机地址 */
alert(url); /* http://127.0.0.1/ */
url = window.location.port; /* 获取端口号 */
alert(url); /* 8080*/
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;
</script>上面就是利用js 获取路径地址参数的一些常用方法,后面还会将一些不常用的分享给大家,希望多多支持喔。
小礼物走一波,支持作者
赏还没有人赞赏,支持一波吧