Sharing global javascript variable of a page with an iframe within that page

Easily by calling parent.your_var_name in your iframe’s script.

One condition: both pages (main and iframe’s) have to be on the same domain.

main page:

<script>
 var my_var="hello world!";
</script>

iframe

<script>
  function some_fn(){
    alert(parent.my_var); //helo world!
  }
</script>

Leave a Comment