美文网首页
iframe 父子页面传递消息

iframe 父子页面传递消息

作者: 奋斗的小小小兔子 | 来源:发表于2018-07-17 17:59 被阅读114次
  1. 父页面和子页面之间传递消息,无跨域问题
// 父页面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','*');
}

相关文章

网友评论

      本文标题:iframe 父子页面传递消息

      本文链接:https://www.haomeiwen.com/subject/jyugpftx.html