Question

I have a dialog like this

<div id="dialog">
  <iframe id="myIframe" src=""></iframe>
</div>
<button id="opener1">Open Dialog</button>
<button id="opener2">Open Dialog</button>

My script is as follows

$(function () {
  $("#dialog").dialog({
    autoOpen: false,
    show: "fade",
    hide: "fade",
    modal: true,
    height: 'auto',
    width: 'auto',
    resizable: true,
    title: 'Vessels'
  });

  $("#opener1").click(function () {
    $('#myIframe').src = 'http://www.w3schools.com';
    $("#dialog").dialog("open");
    return false;
  });

  $("#opener2").click(function () {
    $('#myIframe').src = 'http://www.google.com';
    $("#dialog").dialog("open");
    return false;
  });
});

I want to set the url of the iframe dynamically before the dialog is displayed. I tried the above code, but not working

Was it helpful?

Solution

You can Try this

$(function () {
    $("#dialog").dialog({
        autoOpen: false,
        show: "fade",
        hide: "fade",
        modal: true,
        height: 'auto',
        width: 'auto',
        resizable: true,
        title: 'Vessels',
        close: function( event, ui ) {
           $('#myIframe').attr('src', '');
         }
    });

    $("#opener1").click(function () {
        $('#myIframe').attr('src', 'http://www.w3schools.com');
        $("#dialog").dialog("open");
        return false;
    });

    $("#opener2").click(function () {
        $('#myIframe').attr('src', 'http://www.example.com/');
        $("#dialog").dialog("open");
        return false;
    });
});

While closing the dialog box just simply set the iframe src as empty

Demo : http://jsfiddle.net/94KUB/3/

OTHER TIPS

Try this,

$('#myIframe')[0].src = 'http://www.w3schools.com';

or use attr() like,

$('#myIframe').attr('src','http://www.google.com');

Demo

You can't set the source directly. You have to change the iframe's src attribute like this:

$('#myIframe').attr('src','http://www.w3schools.com');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top