I have an iframe that I run code inside. I don't know the iframes ID as it's set by another application. I wish to change the page in the iframe to another url. How can I do this without knowing the id?
sample html: http://plnkr.co/edit/kE9WdSTWe4GdDKPbFboV?p=catalogue
2 Answers 2
If you know the URL that you want to replace you can do this :
var frames = document.getElementsByTagName("iframe");
for (var i = frames.length - 1; i >= 0; i--)
{
if(frames[i].src == "currentURL"){
frames[i].src = "newURL"
}
}
Or If you are the page inside the iframe you can simply redirect the page :
window.location.replace("http://www.newlocation.com")
10 Comments
window.location.replace("http://www.newlocation.com")If you have a single iframe, do this:
document.getElementsByTagName('iframe')[0].src = "url"
getElementsByTagName returns NodeList, on which you can use index to use the specific element from the list.
Index start at 0
document.getElementsByTagName('iframe')should do