javascript 知道何时提交 iFrame 中的表单的事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6669864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:28:51  来源:igfitidea点击:

Event to know when a form in an iFrame has been submitted

javascripthtml

提问by Jacob

Ok so I have an iframe on the Contact page of my website.

好的,所以我在我网站的联系页面上有一个 iframe。

The src of the iframe points to another file containing a Google Docs form.

iframe 的 src 指向另一个包含 Google Docs 表单的文件。

Once the Google docs form is submitted, the page is redirected to a Google page saying that "your response has been recorded".

提交 Google 文档表单后,页面将被重定向到一个 Google 页面,上面写着“您的回复已被记录”。

I have this form inside an iframe so that it doesn't redirect viewers away from my entire site, but instead only the iframe document is redirected.

我在 iframe 中有此表单,因此它不会将查看者重定向到我的整个站点之外,而是仅重定向 iframe 文档。

This all works fine, But I want to show my own message instead of the Google "your response has been recorded".

这一切正常,但我想显示我自己的消息而不是谷歌“您的回复已被记录”。

To do this, basically I want to know when the iframe has been redirected and/or (preferably) the form has been submitted.

为此,基本上我想知道 iframe 何时被重定向和/或(最好)表单已提交。

Things i've tried...

我尝试过的事情...

  • onhaschange="" inside the iframe element

  • onsubmit="" inside the form element (which is in the iframe src file)

  • iframe 元素内的 onhaschange=""

  • onsubmit="" 在表单元素内(在 iframe src 文件中)

Any other ideas?

还有其他想法吗?

采纳答案by Sascha Galley

Look at this answer: iFrame src change event detection?
Create a function to check if the loaded content is from Google and react properly.

看看这个答案:iFrame src change event detection?
创建一个函数来检查加载的内容是否来自 Google 并做出正确反应。

回答by Aitor Alejandro

Too late I guess but... here's my answer. I've had the same quiz today.

我想为时已晚,但......这是我的答案。我今天有同样的测验。

function getIframe(iframeElement){
    //retrieves the document element inside of an iframe
    return iframeElement.contentDocument;
}

var iframeElem = document.getElementById('iframeId');

iframeElem.onload = function(){
    //onload also catches form post sending
    var iframeDoc = getIframe( iframeTpv );
    //retrieves the height of the content's body
    var contentHeight = iframeDoc.body.scrollHeight;
    //do something ... 
}

回答by mhollingshead

Old question but I think I found a simple solution:

老问题,但我想我找到了一个简单的解决方案:

HTML:

HTML:

<iframe src="google_form_link" onload="changed()">Loading…</iframe>

JS:

JS:

var change_count = 0;

function changed() {
    if (change_count === 1 /* NUMBER OF FORM PAGES */) {

        /* DO SOMETHING */

    }
    else {

        window.change_count += 1;

    }
}