1

i have the following code

var url = "url";
$('<iframe />', {
    name: 'frame',
    id:   'frame',
    src: url
}).appendTo('body');    

but when i click the button, nothing seems to happen

Ken Redler
  • 23,863
  • 8
  • 57
  • 69
BRampersad
  • 862
  • 1
  • 12
  • 25

4 Answers4

1

Make sure you put this code in a document.ready:

$(function() {
    $('#someButtonId').click(function() {
        var url = "url";
        $('<iframe />', {
            name: 'frame',
            id:   'frame',
            src: url
        }).appendTo('body');    
    });
    return false;
});

Live demo.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I think i know what the problem might be. The button is a submit button, is there a way to make the page wait until the iframe loads and then continue along with the submit? – BRampersad Mar 14 '11 at 22:35
1

The code works correctly as it is right now. The bug is probably elsewhere in the code.

http://jsfiddle.net/qrDnH/1/

Peter Olson
  • 139,199
  • 49
  • 202
  • 242
1

Make sure you don't open the same iframe again and again. Check if it exists!

$('#button').click(function() {
    if($('#myIframe').size() == 0) {
        $('<iframe id="myIframe" src="http://stackoverflow.com"></iframe>');
    }
});
  • I think i know what the problem might be. The button is a submit button, is there a way to make the page wait until the iframe loads and then continue along with the submit? – BRampersad Mar 14 '11 at 22:36
  • Unfortunately, as of the last WebKit update, document.write() has been deprecated and there's no callback for the load of an iframe. Fortunately, brilliant users have come up with this: http://stackoverflow.com/questions/164085/javascript-callback-when-iframe-is-finished-loading –  Mar 14 '11 at 22:40
  • I use the iframe code by itself without attaching it to the button and it definitly works so i know the problem is with the submit button. – BRampersad Mar 14 '11 at 22:45
  • I don't actually need a call back, just for the page to stop doing anything until the iframe loads, then it can continue with the "submit". ANy ideas? – BRampersad Mar 14 '11 at 22:47
  • You want it all to wait until the full iframe has been loaded? That's a callback. Can you explain why you want everything to wait? Maybe there's a very easy solution. –  Mar 14 '11 at 22:50
0

its very simple before click on button clear your div innerhtml where is iframe located

like this document.getElementById("mapdive").innerHTML = "";

Azeem
  • 1
  • 1