资源驿站 首页 文章资讯 技术教程

javascript获取页面URL网址链接信息大全

2022-7-18 20:12

下面我们举例一个URL,然后获得它的各个组成部分:http://www.hao123.com/index.jsp?uid=11、window.location.href作用:设置或获取整个 URL 为字符串var test = window.location.href; alert(test); 返回:http://w ...

下面我们举例一个URL,然后获得它的各个组成部分:

http://www.hao123.com/index.jsp?uid=1

1、window.location.href

作用:设置或获取整个 URL 为字符串

var test = window.location.href;
alert(test);

返回:http://www.hao123.com/index.jsp?uid=1

2、window.location.protocol

作用:设置或获取 URL 的协议部分

var test = window.location.protocol;
alert(test);

返回:http:

3、window.location.host

作用:设置或获取 URL 的主机部分

var test = window.location.host;
alert(test);

返回:www.hao123.com

4、window.location.port

作用:设置或获取与 URL 关联的端口号码

var test = window.location.port;
alert(test);

返回:空字符(如果采用默认的80端口(update:即使添加了:80),那么返回值并不是默认的80而是空字符)

5、window.location.pathname

作用:设置或获取与 URL 的路径部分(就是文件地址)

var test = window.location.pathname;
alert(test);

返回:/index.jsp

6、window.location.search

作用:设置或获取 href 属性中跟在问号后面的部分

var test = window.location.search;
alert(test);

返回:?uid=1
PS:获得查询(参数)部分,除了给动态语言赋值以外,我们同样可以给静态页面,并使用javascript来获得相信应的参数值。

7、window.location.hash

作用:设置或获取 href 属性中在井号“#”后面的分段

var test = window.location.hash;
alert(test);

返回:空字符(因为url中没有)

8、js获取url中的参数值

正则法

function getQueryString(name) {
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  var r = window.location.search.substr(1).match(reg);
  if (r != null) {
    return unescape(r[2]);
  }
  return null;
}
// 这样调用:
alert(GetQueryString("参数名"));

split拆分法

function GetRequest() {
   
                       
                    
                    

鲜花

握手

雷人

路过

鸡蛋
粉丝1 阅读460 回复0
国内最专业的资源分享平台
全国免费热线电话

400-123-4567

周一至周日9:00-23:00

反馈建议

api999@qq.com 在线QQ咨询

扫描二维码关注我们

Copyright © 2018-2023 资源驿站 All Rights Reserved.

版权所有:美奇软件开发工作室  备案/许可证号:蜀ICP备19029089号-2

Powered by Discuz! |网站地图