window.location.href获取返回值
时间: 2024-07-31 16:01:28 浏览: 78
`window.location.href` 是JavaScript中用于获取当前网页URL的一个属性。当你通过 `window.location.href` 访问这个属性时,它会返回浏览器窗口的完整URL,包括协议(如http、https)、主机名、路径以及查询字符串等信息。
例如:
```javascript
let currentUrl = window.location.href;
console.log(currentUrl); // 输出:当前页面的实际URL地址
```
如果你想要更改页面的URL,可以直接设置该属性,比如跳转到另一个页面:
```javascript
window.location.href = "https://www.example.com/newpage";
```
相关问题
window.location.href获取参数
window.location.href是一个JavaScript内置对象,用于获取当前页面的URL地址。获取URL中的参数可以通过字符串截取的方式来实现,具体步骤如下:
1. 首先通过window.location.href获取当前页面的URL;
2. 通过indexOf方法获取参数在URL中的位置;
3. 通过substring方法截取出参数所在位置后的字符串;
4. 最后通过split方法将字符串以指定分隔符分割成一个数组,就可以得到所需的参数值了。
下面是一个示例代码:
```
// 获取URL中的参数值
function getUrlParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) {
return decodeURIComponent(r);
}
return null;
}
// 使用示例
var id = getUrlParam('id');
console.log(id);
```
以上代码会获取当前页面URL中名为id的参数值,并输出到控制台中。
window.location.href和window.opener.location.href
window.location.href和window.opener.location.href都是用于获取或设置当前页面的URL。
window.location.href是获取或设置当前页面的URL。可以通过将一个新的URL赋值给window.location.href来导航到该URL。例如,可以使用window.location.href = "https://www.example.com"将当前页面导航到https://www.example.com。
window.opener.location.href是获取或设置打开当前页面的父页面的URL。它在使用window.open()方法打开一个新窗口后才有值。可以通过将一个新的URL赋值给window.opener.location.href来导航到父页面的URL。例如,可以使用window.opener.location.href = "https://www.parentpage.com"将父页面导航到https://www.parentpage.com。
需要注意的是,window.opener.location.href只能在通过window.open()方法打开的新窗口中使用,而不能在同一窗口或不是通过window.open()方法打开的窗口中使用。而window.location.href可以在任何窗口中使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [window.location.href的用法(动态输出跳转)](https://blog.csdn.net/weixin_31690531/article/details/115238595)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]