window.location或window.open如何指定target?
时间: 2023-12-04 19:03:27 浏览: 25
在使用 `window.location` 或 `window.open` 方法时,可以指定 target 参数来指定打开链接的位置。
例如,指定链接在当前窗口中打开:
```javascript
window.location = "http://www.example.com";
```
或者在新窗口中打开:
```javascript
window.open("http://www.example.com", "_blank");
```
其中,`"_blank"` 是一个特殊的 target 值,用于在新窗口中打开链接。其他可用的 target 值包括:
- `_self`:在当前窗口中打开链接,这是默认值。
- `_parent`:在父级窗口中打开链接。
- `_top`:在顶级窗口中打开链接,忽略所有嵌套的窗口。
相关问题
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 ]
window.location.href
window.location.href是用于获取或设置当前页面的 URL 地址。它可以用来打开一个新的页面或刷新当前页面。关于window.location.href的用法,主要有以下几种:使用self.location.href或window.location.href或this.location.href或location.href或parent.location.href可以在当前页面打开一个新的URL页面,或者使用top.location.href可以在顶层页面打开一个新的页面。另外,使用window.open()方法也可以打开一个新的窗口。而window.location.href=window.location.href和window.location.Reload()都可以刷新当前页面,区别在于是否有提交数据。当有提交数据时,window.location.Reload()会提示是否提交,而window.location.href=window.location.href则是向指定的URL提交数据。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [JavaScript中window.open()和Window Location href的区别](https://blog.csdn.net/qq_41694906/article/details/88224861)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [window.location.href](https://blog.csdn.net/zhangweigangweiwu/article/details/52288815)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [undefined](undefined)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]