js中获取window
时间: 2024-03-15 14:45:59 浏览: 19
在JavaScript中,可以通过全局变量`window`来获取浏览器窗口对象,例如:
```javascript
const myWindow = window;
```
在浏览器环境中,`window`对象包含了所有的全局变量、函数和DOM对象,可以通过它来访问和操作浏览器窗口的各种属性和方法。
相关问题
js中window.open获取参数
可以通过以下方式获取新开窗口的参数:
1. 在调用`window.open()`方法时,将参数以字符串形式作为第二个参数传递。例如:
```
window.open("https://www.example.com", "newWindow", "width=400,height=300");
```
其中,第二个参数`"newWindow"`表示新窗口的名称,第三个参数`"width=400,height=300"`表示新窗口的宽度和高度。
2. 在新窗口中使用`window.opener`属性获取父窗口对象,然后通过父窗口对象的`location.search`属性获取参数。例如:
在父窗口中:
```
var newWindow = window.open("https://www.example.com", "newWindow", "width=400,height=300");
newWindow.myParam = "hello";
```
在新窗口中:
```
var param = window.opener.myParam;
console.log(param); // 输出 "hello"
```
这里将参数以属性的形式存储在`window.opener`对象中,然后在新窗口中通过该对象获取参数。注意,该方法只适用于在同一域名下的窗口间传递参数。
js 获取window的长宽
可以使用window对象的innerWidth和innerHeight属性来获取窗口的宽度和高度,示例代码如下:
```javascript
var windowWidth = window.innerWidth;
var windowHeight = window.innerHeight;
console.log("window width: " + windowWidth + ", window height: " + windowHeight);
```
如果想要获取浏览器窗口的整个宽度和高度,包括滚动条和边框,则可以使用document.documentElement的clientWidth和clientHeight属性,示例代码如下:
```javascript
var documentWidth = document.documentElement.clientWidth;
var documentHeight = document.documentElement.clientHeight;
console.log("document width: " + documentWidth + ", document height: " + documentHeight);
```