-1

I have added the iframe code in the Html code which is the default iframe tag of the Dialogflow WebDemo. When you go through the picture which I have uploaded that indicates that when the user types the message and presses enter then automatically it display other response. What I need is when the user presses the enter then I should write the function in the script.

Here is the code of iframe tag in the HTML -

<iframe allow="microphone;" id="myFrame" width="350" height="430" src="dialogflow_bot_link"></iframe>
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Sravya Gajavalli
  • 121
  • 1
  • 10

1 Answers1

0

Try the following

$(document).ready(function(){
  $("iframe").load(function(){
    $(this).contents().on("keyup", function(){
      // handle 'keyup' event
    });
  });
});

iframe tag embeds a separate webpage into the parent webpage. So we use load() to load the contents of the iframe and contents() to get the immediate children within the iframe.

ruth
  • 29,535
  • 4
  • 30
  • 57
  • When I am trying to add the above piece of code I am getting this error "e.indexOf is not a function at w.fn.init.w.fn.load (VM2682 jquery.min.js:2)" – Sravya Gajavalli Mar 13 '20 at 06:12
  • Please see [here](https://stackoverflow.com/a/40252711/6513921). Most probably `load()` is deprecated in your version. Try replacing `load(function(){...})` with `.on('load', function(){ ...})`. – ruth Mar 13 '20 at 06:17
  • No, the error has rectified but it's not entering into the function $(document).ready(function(){ $("iframe").on('load', function(){ $(this).contents().on("keyup", function(){ alert("Wow!") }); }); }); – Sravya Gajavalli Mar 13 '20 at 06:20
  • Do we need to add any attribute in iframe tag – Sravya Gajavalli Mar 13 '20 at 06:20
  • No additional options are needed. It means that the `.on('load',...)` may not be firing as expected. – ruth Mar 13 '20 at 07:34
  • Try passing the function to `$(window)` selector instead of `$(document)` selector. – ruth Mar 13 '20 at 07:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/209557/discussion-between-sravya-gajavalli-and-michael-d). – Sravya Gajavalli Mar 13 '20 at 08:38