Я пытаюсь найти способ перезагрузить свой iframe с другим URL. Код является междоменным, поскольку он включает скрипт, добавленный на сторонний сайт. Я посмотрел на различные методы, чтобы перезагрузить iframe здесь, но ни один из них не работает.
Мой второй подход, я думаю, сработает: удалите элемент iframe, создайте новый элемент с новым URL-адресом, а затем добавьте его обратно в dom. Однако, похоже, он снова не добавляется в dom, не уверен, что это проблема с указателем? Вот примерный план моего кода:
var currentMPId = 0;
function createWindow(mpId) {
var theIframe = document.createElement("iframe");
theIframe.setAttribute("id", "main-iframe");
theIframe.setAttribute("frameborder", "0");
if ((document.getElementById("main-iframe") !== null) && (document.getElementById("main-iframe").src !== "")) { /* don't change the iframe src as it's already been loaded */
if (currentMPId != mpId) { /* if a different id then recreate the iframe */
var d = document.getElementById('container');
var oldframe = document.getElementById("main-iframe");
d.removeChild(oldframe);
theIframe = document.createElement("iframe");
theIframe.setAttribute("id", "main-iframe");
theIframe.setAttribute("src", "http://google.co.uk/");
d.appendChild(theIframe);
}
}
else {
theIframe.setAttribute("src", "http://bing.com");
}
theIframe.style.width = "300px";
theIframe.style.height = "200px";
theIframe.style.position = "absolute";
theIframe.style.top = "0px";
theIframe.frameBorder = 0;
var containerDiv = document.getElementById('container');
containerDiv.appendChild(theIframe);
}
createWindow(10);
Этот метод вызывается несколько раз, но работает только в первом случае, во второй раз он должен открыть Google ..., если "currentMPId! = MpId" имеет значение true.
Может кто-нибудь посоветовать?
Спасибо,
Colin.