0

I tried with a method that was suppose to work.

<html>
<head>
    <script type="text/javascript">
    alert(document.getElementById("gornji").src);

</script>

</head>

<body>
    <iframe id="gornji" src="http://www.w3schools.com/js/default.asp" style="width:100%; height:30%; position:absolute; top:0px; border-style:none"></iframe>


</body> 
</html>

What is wrong here? I am open to all answers.

jpaulik
  • 3
  • 1
  • 3

2 Answers2

2

This is most probably because the iframe is not yet part of the DOM when you try to get its property of src. Either place your script at the very bottom just right before end of body or have it attached to window.onload event.

Check here for a demo http://fiddle.jshell.net/doiks14/72NYm/

andyw_
  • 490
  • 5
  • 15
1

There is no iframe when your code gets executed. Execute your code after DOM is completely loaded.

window.addEventListener("load", function(){
    alert(document.getElementById("gornji").src);
});
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247