A域名下的参数传给B域名下的页面
场景1:推广页面要求一定要使用A域名,而推广页面的网页内容是在B域名下,而B域名推广页需要从A域名的推广链接上获取参数。
这里是填写的推广链接是A域名下的,但是实际用户访问的页面是B域名下的
game.html页面
例如:
A域名的推广链接:http://A.com/iframeParentPage.html?app_id=70116&user=admin&age=18
B域名推广页面:http://B.com/game.html?game_id=10002&platform_id=205&type=game
A、B域名在不同的主体下,不能把B域名下的页面放到A域名的服务器上。
B域名需要拿到
app_id等参数。
- A域名下 - iframeParentPage.html中获取参数:- window.location.search- 1 - console.log(window.location.search); // ?app_id=70116&user=admin&age=18 
- 携带A域名下 - iframeParentPage.html中的参数,进行页面跳转- 1 - window.location.href = "http://B.com/game.html?game_id=10002&platform_id=205&type=game&" + window.location.search.substring(1); - 备注: - 使用 - window.location.search.substring(1);是要把- ?去掉
- 要在 - type=game后面加上- &
 
- 在B域名下的 - game.html页面中获取参数- 1 
 2
 3
 4
 5
 6- const queryParams = new URLSearchParams(window.location.search); 
 console.log('queryParams');
 console.log(queryParams);
 console.log(queryParams.get('app_id'));
 console.log(queryParams.get('user'));
 console.log(queryParams.get('age'));
iframe嵌套页面-父子页面传参
查询字符串(URL 参数)
场景2:推广页面要求一定要使用A域名,而推广页面的网页内容是在B域名下,而B域名推广页需要从A域名的推广链接上获取参数。
这里是填写的推广链接是A域名下的,但是实际用户访问的页面是A域名下
iframe嵌套了B域名下的game.html的页面,也就是URL中显示的是A域名
A域名下的页面:
| 1 | <iframe title="游戏页面" src="" frameborder="0"></iframe> | 
| 1 | document.querySelector('iframe').src= "http://A.com/iframeParentPage.html?app_id=70116&user=admin&age=18&" + window.location.search.substring(1); | 
B域名下的页面:
| 1 | const queryParams = new URLSearchParams(window.location.search); | 
同域名下-使用缓存
A页面:
| 1 | localStorage.setItem('params', window.location.search.substring(1)); | 
B页面:
| 1 | var params = localStorage.getItem('params'); | 
注意:不同域名,使用这个方法不可以。
根据需要,使用
localStorage或sessionStorage
