0

I'm trying to execute a javascript function that would change the source code for each iframe. It works if the iframe is not within any other tags.

<!DOCTYPE html>
<html>
<head>
<link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<button onclick="myFunction()">Search</button>
<iframe src="https://www.espn.com"  frameborder="1"  width="500" height="320"></iframe>
<iframe src="https://www.nbc.com"  frameborder="1"  width="500" height="320"></iframe>
<iframe src="https://www.cnn.com"  frameborder="1" width="500" height="320"></iframe>
 <script type = "text/javascript">
  function myFunction(){
  alert("Hello!");
  var frames = window.frames;
  for (var i = 0; i < frames.length; i++){
    frames[i].location = "http://www.w3schools.com";
    }  
}
</script>
</body>
</html>

How ever if I put the iframe within div tags the javascript for some reason doesn't reach them.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
<link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
 <div class="container-fluid">
    <form align = "center">
      <br>
      <button onclick="myFunction()">Search</button>
      <br>
    </form>
<div class="col-xs-8 col-lg-6">
    <h3 id = "ESPN">ESPN</h3>
       <iframe src="https://www.espn.com"  frameborder="1"  width="500" height="320"></iframe>
      <br>
    </div>
    <div class="col-xs-8 col-lg-6">
    <h3 id = "NBC">NBC</h3>
      <iframe src="https://www.nbc.com"  frameborder="1"  width="500" height="320"></iframe>
      <br>
    </div>
    <div class="col-xs-8 col-lg-6">
    <h3 id = "CNN">CNN</h3>
      <iframe src="https://www.cnn.com"  frameborder="1" width="500" height="320"></iframe>
      <br>
</div>
</div>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
 <script src="js/bootstrap.min.js"></script>
 <script type = "text/javascript">
  function myFunction(){
  alert("Hello!");
  var frames = window.frames;
  for (var i = 0; i < frames.length; i++){
    frames[i].location = "http://www.w3schools.com";
    }  
}
</script>
</body>
</html>

Is there something else I need to do in order for my function to read the iframes?

4 Answers 4

2
  function myFunction(){
  alert("Hello!");
  var frames = window.getElementsByTagName("iframe"); // You only want iframes
  for (var i = 0; i < frames.length; i++){
    frames[i].src = "http://www.w3schools.com";
    }  
}
Sign up to request clarification or add additional context in comments.

Comments

1
 var frames = window.querySelectorAll("iframe"); 

to get all iframes. Im not sure but you may also change your line to
frames[i].src = "http://www.w3schools.com";

Comments

0

Try this

frames[i].src= "http://www.w3schools.com";

insted of that

frames[i].location = "http://www.w3schools.com";

Comments

0

You need to replace your javascript with the following:

function myFunction() {
            alert("Hello!");
            var frames = document.getElementsByTagName("iframe");
            for (var i = 0; i < frames.length; i++) {
                frames[i].src = "http://www.w3schools.com";
            }
        }

2 Comments

For clarity, could you explain what the mistake in the original code was that your solution addresses?
Well, there's nothing wrong in your code. Bt since document.getElementByTagName is restricted to look in only the document object of which it is a method, It is more elegant method. By the way, your code worked without any problem here...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.