window.open父子画面传值
时间: 2023-07-28 07:12:31 浏览: 55
可以通过以下步骤实现父子页面之间的传值:
1. 在父页面中使用 `window.open` 方法打开子页面,并保存子页面的对象引用。
```javascript
var childWindow = window.open("child.html");
```
2. 在子页面中使用 `opener` 属性获取父页面的对象引用,并调用它的方法传递数据。
```javascript
opener.receiveData(data);
```
3. 在父页面中定义 `receiveData` 方法接收子页面传递的数据。
```javascript
function receiveData(data) {
// 处理传递的数据
}
```
完整的代码示例:
父页面:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>父页面</title>
</head>
<body>
<button onclick="openChild()">打开子页面</button>
<script>
var childWindow;
function openChild() {
childWindow = window.open("child.html");
}
function receiveData(data) {
console.log("收到子页面传递的数据:" + data);
}
</script>
</body>
</html>
```
子页面:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>子页面</title>
</head>
<body>
<button onclick="sendData()">传递数据给父页面</button>
<script>
function sendData() {
var data = "Hello, parent!";
opener.receiveData(data);
}
</script>
</body>
</html>
```