0

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.)

Community
  • 1
  • 1
Mechanical snail
  • 29,755
  • 14
  • 88
  • 113

1 Answers1

1

Take this line out:

elem_iframe.contentWindow.location.reload();

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

Digital Plane
  • 37,354
  • 7
  • 57
  • 59
  • This seems to fix the problem, but do you know why it works? Is `.reload()` supposed to restore the original (i.e. from the HTML source) attributes of the iframe? – Mechanical snail Aug 30 '11 at 04:14
  • 1
    @Mechanical snail It's probably some kind of a race condition, where the `src` is being changed, but the `reload()` happens before the navigation takes effect - therefore it stays on the same page. – Digital Plane Aug 30 '11 at 04:24