How do I execute code after iframe reloaded?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • postman
    New Member
    • Nov 2008
    • 63

    How do I execute code after iframe reloaded?

    I am writing a client-side search function for a page which searches the content in a separate file (same domain) that is displayed in an iframe.

    I'm trying to code it so that when the user runs the search, the iframe will first be reloaded, then the search is run.

    The iframe reloads, but the search is performed before the reload is complete and the updated content isn't included in the search.

    How can you run code after the iframe has completely reloaded?

    Thanks in advance.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Run the search iframe onload, so attach an onload event handler to the iframe.

    Comment

    • postman
      New Member
      • Nov 2008
      • 63

      #3
      Thank you for pointing me in the right direction.

      I solved it using the following code:

      Script:
      Code:
      var searching = 0; //Flag to indicate whether search is being run
      
      function frameLoad() {
        if (searching == 1) {
          searchText();
          searching = 0;
        }
      }
      
      function frameRefresh() {
        searching = 1;
        window.frames['myiframe'].contentWindow.location.reload(true);
      }
      
      function searchText(){
        ...
      }
      In the HTML, I used the onload attribute.
      Code:
      <iframe id="myiframe" src="page.html" frameborder=0 onload="frameLoad();"></iframe>
      The "Search" button or link calls the "frameRefre sh" function.

      Not sure which is better--to attach the event when the search is executed or hardcode the onload attribute and use a variable to check if search is being run (to prevent it from searching on the initial page load).

      Thanks for the help!

      Comment

      Working...