How do I load an url in iframe with Jquery

后端 未结 5 1446
日久生厌
日久生厌 2020-11-28 04:14

I want to load an iframe on click, this is what I have so far:

$(\"#frame\").click(function () { 
      $(\'this\').load(\"http://www.google.com/\");
    });         


        
相关标签:
5条回答
  • 2020-11-28 04:52

    Just in case anyone still stumbles upon this old question:

    The code was theoretically almost correct in a sense, the problem was the use of $('this') instead of $(this), therefore telling jQuery to look for a tag.

    $(document).ready(function(){
      $("#frame").click(function () { 
        $(this).load("http://www.google.com/");
      });
    });
    

    The script itself woudln't work as it is right now though because the load() function itself is an AJAX function, and google does not seem to specifically allow the use of loading this page with AJAX, but this method should be easy to use in order to load pages from your own domain by using relative paths.

    0 讨论(0)
  • 2020-11-28 04:59
    $("#button").click(function () { 
        $("#frame").attr("src", "http://www.example.com/");
    });
    

    HTML:

     <div id="mydiv">
         <iframe id="frame" src="" width="100%" height="300">
         </iframe>
     </div>
     <button id="button">Load</button>
    
    0 讨论(0)
  • 2020-11-28 05:03

    Try $(this).load("/file_name.html");. This method targets a local file.

    You can also target remote files (on another domain) take a look at: http://en.wikipedia.org/wiki/Same_origin_policy

    0 讨论(0)
  • 2020-11-28 05:10

    here is Iframe in view:

    <iframe class="img-responsive" id="ifmReport" width="1090" height="1200" >
    
        </iframe>
    

    Load it in script:

     $('#ifmReport').attr('src', '/ReportViewer/ReportViewer.aspx');
    
    0 讨论(0)
  • 2020-11-28 05:16
    $("#frame").click(function () { 
        this.src="http://www.google.com/";
    });
    

    Sometimes plain JavaScript is even cooler and faster than jQuery ;-)

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