So I am making a page which will allow me to render outside pages, so that I can begin to manipulate data and refresh content periodicly (ideally, not what this is about though).
So my problem is that I have a little javascript to call pages to load to my iframe, but when I attempt to apply my url to the src attribute of my iframe, it doesn't seem to stick, and no outside page loads. I have been testing this using my own website, so there should be no restrictions in place from that url location to prevent it from loading. I am not sure if the page needs to reload (possibly?) for the iframe to change its src? If so, my idea will be totally unfeasible, so I hope that isn't the issue. Here is my code thus far:
<head>
<style>body{text-align:center;}#targetField{width:80%;margin:3px;}</style>
<script>
var frames = new Array();
var url;
function direct(url){
frames = document.getElementsByTagName('iframe');//Obtain all iframe elements
for(i=0;i<frames.length;i++){//For each frame
//IF the frames source element is blank or null
if(frames[i].getAttribute('src') === null || frames[i].getAttribute('src') === 'null' || frames[i].getAttribute('src') === ''){
frames[i].setAttribute('src', url);
}
else if(frames[i].getAttribute('src') === url){
//IF the souce element already has a target and is SAME as url
if(navigator.appName == 'Microsoft Internet Explorer'){
//If IE then use reload()
frames[i].contentWindow.location.reload(true);
}
else{
//Otherwise set source to reload
frames[i].src = frames[i].src
}
}
else{}
}
}
</script>
</head>
<body>
<form>
<input id="targetField"></input>
<button onclick="direct(document.getElementById('targetField').value);" value="submit">GO!!!</button>
</form>
<iframe id="iF01"></iframe></body></html>
I would rather do this with pure javascript without using any jQuery if possible, I am just not sure why my debug console shows no issues, but does nothing. Tested in Chrome, FF and IE8. JSFiddle here http://jsfiddle.net/77tx44od/2/
I have also tried this now, to no avail, creating a new iFrame to load the content via its src attribute.
<script>
var frames = new Array();
var url;
var x = 0;
function direct(url){
frames = document.getElementsByTagName('iframe');//Obtain all iframe elements
for(i=0;i<frames.length;i++){//For each frame
frames[i].parentnode.removeChild(frames[i]);
}
var newFrame = document.createElement('iframe');
newFrame.setAttribute('src', url);
newFrame.setAttribute('id', 'iF'+x);
document.body.appendChild(newFrame);
x++;
}
</script>
iframe[i]where you should haveframes[i]- that does indeed produce an error in the developer console.