1

consider the below given two HTML pages

parent.html

<!DOCTYPE html>
<html>
<body>
<button id='parent' onclick="test()">Click Me</button>
<div  class='test-ab' id="load"></div>
</body>

<script>
  function test()
  {
    //need to call child function or button
  }

  $(function(){
    $('#load').html('<object id="webloader" data="child.html"/>');
  });
</script>
</html>

child.html

<!DOCTYPE html>
<html>
<body>
<button id='child' onclick="helloworld()">Click Me</button>
</body>
<script>
  function helloworld()
  {
    alert("hello world");
  }
</script>

</html>

there are two defined HTML pages defined, is there any way to call the child.html function (eg here:-helloworld()) from parent.html

paradox
  • 153
  • 12
  • 1
    Assuming both pages are on the same domain then you could use an `iframe`: https://stackoverflow.com/questions/1600488/calling-javascript-function-in-iframe. A better method entirely would be to create your own library of common functions and include it in both pages. – Rory McCrossan Jul 10 '18 at 09:40
  • current I have loaded it through the object tag, currently the function is defined under child.html only – paradox Jul 10 '18 at 09:42
  • why don't use put all javascript in one file and load it in both html pages ? – madjaoue Jul 10 '18 at 09:42
  • @paradox yep, and it won't work that way – Rory McCrossan Jul 10 '18 at 09:43
  • i could access parent functions through parent.function() in child.html, in the same way i thought we could for parent to child access – paradox Jul 10 '18 at 09:45

2 Answers2

1

You could try the postMessage mechanism

Regards, Vincent

vcaraccio
  • 76
  • 5
0

You can try this:

let child_element = $("#parent").find(".load");
Ankit
  • 562
  • 5
  • 12