简体   繁体   中英

Setting content of iframe using javascript fails in firefox

I'm trying to set the content of a iframe using javascript.

I've the html string. And I'm writing:-

 var iframe = $('iframe')[0],
 content = '<div>test</div>';        
 $(iframe).contents().find('html').html(content);

It works fine in google chrome, but in firefox is shows the content for a moment and the disappears. Please help me to fix it.

I faced the same problem. I saw the load function of iframe fires in firefox but not in chrome. So firefox reload the iframe again on load event.

So with your existing code try this:-

   $(iframe).load(function(e){
      $(iframe).contents().find('html').html(content);

    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM