最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to detect user has completed embedded Typeform? - Stack Overflow

programmeradmin1浏览0评论

I have a Typeform embedded into my page inside an iframe. From my JS I would like to know that user has finished the typeform (i.e. clicked Submit). The typeform JS does not seem to provide any events one can listen to. Currently I figured out only following solution -- to call periodically following test to detect that the outro page is being displayed:

document.getElementById('my-typeform-iframe').getElementsByClassName('outro').length > 0

Is there some nicer approach?

I have a Typeform embedded into my page inside an iframe. From my JS I would like to know that user has finished the typeform (i.e. clicked Submit). The typeform JS does not seem to provide any events one can listen to. Currently I figured out only following solution -- to call periodically following test to detect that the outro page is being displayed:

document.getElementById('my-typeform-iframe').getElementsByClassName('outro').length > 0

Is there some nicer approach?

Share Improve this question edited Mar 28, 2018 at 15:52 picsoung 6,5191 gold badge20 silver badges36 bronze badges asked Feb 7, 2017 at 11:01 PeterPeter 2,4423 gold badges23 silver badges30 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 11

ok, so it I was wrong in my assumptions above - it turns out the typeform iframe emits an event when user submits the form. the event is named form-submit and one can process it e.g. this way:

    window.addEventListener('message', function(event){
      if(event.data == 'form-submit')
        // your business logic here
    }, false);

Typeform now has a 'onSubmit' callback feature for that.

<script src="https://embed.typeform.com/embed.js"></script>
<script>
  window.addEventListener("DOMContentLoaded", function() {
    var el = document.getElementById("my-embedded-typeform");
    window.typeformEmbed.makeWidget(el, "https://admin.typeform.com/to/cVa5IG", {
    onSubmit: function() {
      alert("submited!");
    }
  });
});

Here's the documentation

Apparently there's no easy way to retrieve the answers. As mentioned by @matthew, a response_id is passed to the callback (I guess), and you can query the API with that but you need to implement a retry loop because responses are not immediately available.

Thanks to peter answer, I checked the event they provided and get few good results. here is my findings:

  window.addEventListener('message', function(event){
  if(event.data.type == 'form-submit')
    // your business logic here
}, false);

The event data have a couple of types which is:

  • form-theme
  • TARP
  • form-ready
  • form-height-changed
  • form-screen-changed
  • A JSON event with no type that have the form answers
  • A JSON event with the form submit information
  • form-submit
  • embed-auto-close-popup
发布评论

评论列表(0)

  1. 暂无评论