Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an iframe using jQuery, and display on page?

Tags:

jquery

$(document).ready(function(){     $('#button').click(function(){        $('.accordion').load('<iframe src="google.com" frameborder="0" scrolling="no" id="myFrame"></iframe>');     }); }); 

This can't display the iframe of google.com. Can anyone provide suggestions on how to fix it?

like image 949
Hai Truong IT Avatar asked Nov 18 '11 08:11

Hai Truong IT


People also ask

Can you use jQuery in an iframe?

However, it's also possible to use a really simple jQuery to access the elements within the iFrame. Check it out below: $(document). ready(function(){ var iFrameDOM = $("iframe#frameID").

How can get iframe HTML using jQuery?

# jQuery Code to Get HTML Content of IFrame$("#myButton"). on('click', function() { alert($("#myIframe"). contents(). find("html").

How do I add HTML content to an iframe?

Answer: Use the jQuery contents() method You can use the jQuery contents() method in combination with the find() , val() and html() methods to insert text or HTML inside an iframe body.


1 Answers

jQuery's load function isn't used this way. It takes a URL as a parameter, among others, and loads the response from that URL.

If you are trying to create an iframe DOM element, use the jQuery function to do this and append it where you want:

$('<iframe src="http://google.com" frameborder="0" scrolling="no" id="myFrame"></iframe>')      .appendTo('.accordion'); 

or

$('<iframe>', {    src: 'http://google.com',    id:  'myFrame',    frameborder: 0,    scrolling: 'no'    }).appendTo('.accordion'); 
like image 80
Andy Rose Avatar answered Sep 29 '22 06:09

Andy Rose