用window.open()打开新页面并该页面传值
情景:
需要带参数的打开一个新的页面,但是会发现根据官方的文档,window.open()里面可以携带的四个参数并没有可以传值的。
https://www.runoob.com/jsref/met-win-open.html
但是可以这样子传,这是父窗口的部分代码:
//这个ReportFlag为需要传递的变量
window['reportFlag'] = ReportFlag
//然后打开子窗口,具体路径根据实际改变
window.open('/goToPrint.html')
这是子窗口的部分代码:
let reportFlag = window.opener['reportFlag']
亲测有效。
当然也可以传递一个对象。
具体可以参考这个
//要发送的参数
let params = {
"type": "red",
"id": 25,
"name": "weekdawn"
};
window["filter"] = params;
window.open("https://blog.csdn.net/weekdawn");
let receive = window.opener["filter"];
//获取接收到的数据
let type = receive["type"];
let id = receive["id"];
let name = receive["name"];
eceive["id"];
let name = receive["name"];
``