js获取当前Url网址路径及参数 location.hostname

本文详细解析了通过JavaScript获取网页URL中参数的方法,并展示了如何利用正则表达式从URL查询字符串中提取特定参数值。

输入的网址是(没有框架):http://localhost:81/Test/1.htm?Did=123
<br>以下为输出:  
<br>
<SCRIPT>

//获取Url传过来的值  
function Request(name)  
{  
     new RegExp("(^|&)"+name+"=([^&]*)").exec(window.location.search.substr(1));  
     return RegExp.$2  
}  

thisURL = document.URL;     // http://localhost:81/Test/1.htm?Did=123
thisHREF = document.location.href; // http://localhost:81/Test/1.htm?Did=123
thisSLoc = self.location.href;   // http://localhost:81/Test/1.htm?Did=123
thisDLoc = document.location;   // http://localhost:81/Test/1.htm?Did=123

thisTLoc = top.location.href;   // http://localhost:81/Test/1.htm?Did=123
thisPLoc = parent.document.location;// http://localhost:81/Test/1.htm?Did=123
thisTHost = top.location.hostname; // localhost  
thisHost = location.hostname;   // localhost  

thisU1 = window.location.protocol; // http:  
thisU2 = window.location.host;   // localhost:81  
thisU3 = window.location.pathname; // /Test/1.htm  

document.writeln( thisURL + "<br />");   
document.writeln( thisHREF + "<br />");   
document.writeln( thisSLoc + "<br />");   
document.writeln( thisDLoc + "<br />");  

document.writeln( thisTLoc + "<br />");   
document.writeln( thisPLoc + "<br />");   
document.writeln( thisTHost + "<br />");   
document.writeln( thisHost + "<br />");  

document.writeln( thisU1 + "<br />");   
document.writeln( thisU2 + "<br />");   
document.writeln( thisU3 + "<br />");  

document.writeln( "DidDid="+Request("Did") );// Did=123
</SCRIPT>


### JavaScript 获取当前页面的 URL 方法 在 JavaScript 中,可以通过 `window.location` 对象获取当前页面的完整 URL 或其部分信息。以下是几种常用的方法: 1. **获取完整的 URL** 使用 `window.location.href` 可以直接获取当前页面的完整 URL 地址[^2]。 ```javascript var fullUrl = window.location.href; console.log(fullUrl); ``` 2. **获取 URL路径部分** 如果只需要获取 URL路径部分(不包含查询参数和锚点),可以使用 `window.location.pathname`[^3]。 ```javascript var path = window.location.pathname; console.log(path); ``` 3. **获取查询参数部分** 查询参数部分可以通过 `window.location.search` 获取。 ```javascript var queryParameters = window.location.search; console.log(queryParameters); ``` 4. **获取 URL 的主机名** 如果需要获取当前页面所在的主机名,可以使用 `window.location.hostname`[^2]。 ```javascript var hostName = window.location.hostname; console.log(hostName); ``` 5. **获取 URL 的端口号** 端口号可以通过 `window.location.port` 获取[^2]。 ```javascript var port = window.location.port; console.log(port); ``` 6. **获取 URL 的协议类型** 协议类型(如 `http:` 或 `https:`)可以通过 `window.location.protocol` 获取[^2]。 ```javascript var protocol = window.location.protocol; console.log(protocol); ``` 7. **获取 URL 的锚点部分** 锚点部分可以通过 `window.location.hash` 获取。 ```javascript var hash = window.location.hash; console.log(hash); ``` 8. **解析 URL 参数** 如果需要解析 URL 中的查询参数,可以使用自定义函数或内置方法。以下是一个简单的实现示例[^4]: ```javascript function GetUrlParam(paraName) { var url = document.location.toString(); var arrObj = url.split("?"); if (arrObj.length > 1) { var arrPara = arrObj[1].split("&"); var arr; for (var i = 0; i < arrPara.length; i++) { arr = arrPara[i].split("="); if (arr != null && arr[0] == paraName) { return arr[1]; } } return ""; } else { return ""; } } var id = GetUrlParam("id"); console.log(id); ``` 通过以上方法,可以根据需求灵活地获取当前页面的 URL 及其各个组成部分。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值