- 父页面和子页面之间传递消息,无跨域问题
// 父页面farther.html
<iframe id="form-apply"
src="child.html"
width="800"
height="500"
frameborder="0"
scrolling="no"></iframe>
...
// 接收传递过来的消息
window.addEventListener('message',function(e){
let string=e.data;
switch(string) {
case 'close':
closeModal();
break;
case 'success':
applySuccess();
break;
default:
applySuccess();
}
},false);
function closeModal() {
let $modalForm = document.getElementById('modal-form');
$modalForm.style.display = "none";
}
function applySuccess () {
window.frames['form-apply'].src= "http://c.sit.lattebank.com/loanweb/applySuccess";
}
// 子页面中child.html
$('.submit').on('click', (e) => {
// 给父页面传递'success'的字符串
window.parent.postMessage('success','*');
}
网友评论