Replace Entire Content of Iframe

前端 未结 4 1160
鱼传尺愫
鱼传尺愫 2020-12-29 08:11

Is there a way to replace the ENTIRE contents of an iframe using Javascript (or Jquery)?

For example if my iframe had the following content...

blah o         


        
4条回答
  •  南笙
    南笙 (楼主)
    2020-12-29 08:14

    have a similar scenario, where I am generating a new iFrame with content from a different domain each time the row of a table is clicked.

    To do this, I use jQuery AJAX and PHP. PHP looks at my MySQL table and gets the URL of the iFrame I want to load; it generates the HTML for an iFrame, and I use jQuery AJAX to replace that content.

    My HTML looks something like this:

    And my AJAX call looks like this:

    function loadPage(record_id) {
    
        $.ajax({
            type: "POST",
            url: "php/ajax_handler.php",
            data: "action=makeIFrame&record_id=" + record_id,
            success: function(msg){
                $('.xFrame' ).html(msg);
            }
        });
    }
    

    The code replaces the HTML of the div that contains the iFrame with a new iFrame.

    Hope this (or something similar) works for you.

提交回复
热议问题