Mechanical snail
Mechanical snail

Reputation: 30637

Change src of iframe from a popup window

In my web application, the user can open a popup window to select an edit an object. When the user presses OK on the popup, it's supposed to update the src of an iframe in the parent window (and of course reload the iframe) according to which object was selected.

My function (in the parent window) is:

function dismissEditPopup(win, newId) {
    newId = html_unescape(newId);
    var elem_iframe = document.getElementById("iframe_id");

    // (*) this line doesn't work
    elem_iframe.src = '/view_object/' + newId;

    elem_iframe.contentWindow.location.reload();

    win.close();
}

This function is called from a popup window, which contains a script:

<script type="text/javascript">
    opener.dismissEditPopup(window, "hash_of_new_object");
</script>

The problem is that the line (*) fails silently. In the inspector in both Firefox 3.6 and Google Chromium, I see that the src attribute of the iframe is being updated, but elem_iframe.contentWindow.location.href is unchanged. (If I add a line elem_iframe.contentWindow.location.href = elem_iframe.src;, the assignment is ignored.). There are no errors in the Javascript error console. Strangely, it does work as expected if I assign to elem_iframe.src from the Javascript console.

I am able to change the value of a hidden <input> field in the same way, using document.getElementById("hidden_id").value = newId;.

Everything is served from the same website.

(Similar to Changing iframe src with Javascript, but the answers to that question don't work, presumably because the code is called from a popup.)

Upvotes: 0

Views: 1941

Answers (1)

Digital Plane
Digital Plane

Reputation: 38264

Take this line out:

elem_iframe.contentWindow.location.reload();

It's reloading the iframe and the new src is not loaded.

Upvotes: 1

Related Questions