Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document.ready function is not working inside an iframe

In the parent page I have an iframe like this:

<iframe src="facts.php" style="width:320px; height:500px; border:hidden" id="facts">  </iframe>

And inside of that iframe I have a jQuery function like this,

<script type="text/javascript">
$(document).ready(function(){
    $('#demo').hide();
    $('.vticker').easyTicker();
});
</script>

But this function doesn't trigger when the parent page is loaded. Can you please explain me how to solve this?

like image 947
Izu Avatar asked Nov 19 '13 00:11

Izu


Video Answer


2 Answers

Try this inside your iframe.

<script type="text/javascript" src="js/Jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('#demo').hide();
    $('.vticker').easyTicker();
});
</script>

You are facing this issue because the parent JavaScript cannot access the events inside the iframe. You better include the script inside the source of the iframe.

like image 58
Naresh Pansuriya Avatar answered Nov 15 '22 00:11

Naresh Pansuriya


This seems like cross side scripting to me. First of all try this in your iframe:

<script>
    $(document).ready(function()
    {
        alert("iFrame is ready");
    });
</script>

If this does not work, check if you includet jQuery the right way.

Remember: your iframe is a complete selfstanding webside. There is no communication between your main frame and your iframe. That means if you use Javascript in your iframe you can only manipulate HTML-code inside the iframe. If you want to manipulate HTML-elements outside of the iframe you need to include the Javascript in your main frame.

like image 30
dorr Baume Avatar answered Nov 14 '22 22:11

dorr Baume