JQuery and frames - $(document).ready doesn't work

后端 未结 12 2322
长发绾君心
长发绾君心 2020-12-14 09:51

I have a page, with some code in js and jQuery and it works very well. But unfortunately, all my site is very very old, and uses frames. So when I loaded my page inside a fr

相关标签:
12条回答
  • 2020-12-14 10:03

    I have tried the method mentioned in another comment:

    $("#frameName").ready(function() {
        // Write you frame on load javascript code here
    } );
    

    and it did not work for me.

    this did:

    $("#frameName").load( function() {
         //code goes here
    } );
    

    Even though the event does not fire as quickly - it waits until images and css have loaded also.

    0 讨论(0)
  • 2020-12-14 10:04

    This answer may be late, but this reply may help someone like me...

    This can be done via native Javascript code -

    ifrm2 = var ifrm2 = document.getElementById('frm2');
    if (ifrm2.contentDocument.readyState == 'complete') {
      //here goes the code after frame fully loaded
    }
    
     //id = frm2 is the id of iframe in my page
    
    0 讨论(0)
  • 2020-12-14 10:08

    I assume this is a similar problem I was having with DOMContentLoaded in an iframe.

    I wrote a blog post about it.

    0 讨论(0)
  • 2020-12-14 10:09

    If you want to fire the onload event for your frames, then follow these steps:

    1. Assign an id and name to each <frame> tag. Make sure both id and name attributes value is same.

    2. Use the following code to fire the onload event of the frame:

      $("frameName").ready(function() { 
        // Write your frame onload code here
      }
      
    0 讨论(0)
  • 2020-12-14 10:14

    No need to modify the markup. Just fix the selector. It should be:

    $("frame[name='main']").ready(function(){..}); 
    

    not

    $("#frameName").ready(function(){..}); 
    

    Note: it seems the jQuery ready event fires multiple times. Make sure that is OK with your logic.

    0 讨论(0)
  • 2020-12-14 10:19

    There is no reason for $(document).ready() not to be called. Be sure your page contains an include to jquery.js. Try to do a simple test with an empty HTML page and just an alert to see if there is another problem.

    If you are trying to use this inside the HTML page that contains the frame's definition, keep in mind that there is no document there, you will have to use the

    0 讨论(0)
提交回复
热议问题