2

Hello I am trying to update the src of an iframe with javascript, below is the code I am trying. When I try to execute the code it fails as if an error occured, and stops executing code when I call this line.

document.getElementById('logviewer').src = "www.yahoo.com";

Example code:

<iframe src="" id="logviewer"/>
<script type="javascript/text">
document.getElementById('logviewer').src = "www.yahoo.com";
alert('Loaded Yahoo');
</script>

You can see the src doesn't change and the alert doesn't show.

Thanks ahead of time for your answers!

1 Answers1

2

There are some major issues in your code:

  • script mime-type should be text/javascript not javascript/text
  • iframe tag should have a closing tag...shortopentags are not supported. This is because contant in between iframe tags is shown when the browser does not support iframes. Your code meant that the script tag ended up in this section instead.
  • document might not be ready when that code runs (you need to put your code in a domready event), this is basically what Patrick is talking about. I think it's a very relevant cause as well.

Here's your code in a working condition: http://jsfiddle.net/4EMdn/

Christian
  • 27,509
  • 17
  • 111
  • 155