Solved

Get URL from Iframe using Javascript

Posted on 2011-03-16
7
412 Views
Last Modified: 2012-05-11
Hi,

I'll wanting to find a div (with ID) on the page then look for the Iframe inside that div. Once the iframe is found, to get the URL (src attr) and write this to the next button on the the page.

I seem to be having issues finding the iframe itselfs, can anyone give me some help with this.
Thanks.

<html>
<div id="nodeWrapper">
<iframe title="YouTube video player" width="400" height="280" src="http://www.youtube.com/embed/cWSdOIqrgXw?rel=0" frameborder="0" allowfullscreen></iframe> 

<a href="iframeurl">Link </a>

</div>

<div id="nodeWrapper">
<iframe title="YouTube video player" width="400" height="280" src="http://www.youtube.com/embed/cWSdOIqrgXw?rel=0" frameborder="0" allowfullscreen></iframe> 

<a href="iframeurl">Link </a>

</div>

<div id="nodeWrapper">
<iframe title="YouTube video player" width="400" height="280" src="http://www.youtube.com/embed/cWSdOIqrgXw?rel=0" frameborder="0" allowfullscreen></iframe> 

<a href="iframeurl">Link </a>

</div>



</html>

Open in new window


 
<script type='text/javascript'> 
window.onload=init; 
function init()
{
 
  var o = document.getElementById("videoContent");
  var p = o.getElementsByTagName('iframe');
  if(p)
  {
    for( var i=0,limit=p.length; i < limit; ++i )
    {
      var URL;
      p[i].src = URL;
      //break;
      alert(URL);

      //split URL to get video ID
      //Add Video ID to next button with ID (button)
      // Do this for all 10 buttons on the page


    } 
  }
}
//--></script>

Open in new window

0
Comment
Question by:Envision11
  • 3
  • 3
7 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 35153473
This will get you a little closer:

<script type='text/javascript'> 
window.onload=init; 
function init()
{
 
  var p = document.getElementsByTagName('iframe');
  if(p)
  {
    for( var i=0,limit=p.length; i < limit; ++i )
    {
      alert(p[i].src);


    } 
  }
}
//--></script>

Open in new window

0
 

Author Comment

by:Envision11
ID: 35154975
Thanks for the comment, but this will return all iframes on the page. The code above works, but I need to find an iframe within a div not all iframes.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 35156732
line 7 :  var p = o.getElementsByTagName('iframe')[0];
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 82

Expert Comment

by:leakim971
ID: 35156763
var URL = o.getElementsByTagName('iframe')[0].src;
0
 

Author Comment

by:Envision11
ID: 35158141
OK so I have changed the code to what has been suggested, this will only return the first iframe from the first div. How is it possible to find all the iframes with each div and return them to an array?

window.onload=init; 
function init()
{
  var o = document.getElementById("videoContent");
  for (var i = 0, limit = o.length; i < limit; ++i) {
    if (o) {
        var URL = o.getElementsByTagName('iframe')[0];
        alert(URL.src);

        //split URL to get video ID
        //Add Video ID to next button with ID (button)
        //Do this for all 10 buttons on the page
    }
  }
}

Open in new window

0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 500 total points
ID: 35159601
first use the << class >> attribute instead the << id >>
ID must be unique in a document.

<div class="nodeWrapper">

Open in new window




window.onload=init; 
function init()
{
  var o = document.getElementsByTagName("div");
  for (var i = 0, i<o.length; i++) {
    if (o.className == "nodeWrapper" ) {
        var URL = o.getElementsByTagName('iframe')[0];
        alert(URL.src);

        //split URL to get video ID
        //Add Video ID to next button with ID (button)
        //Do this for all 10 buttons on the page
    }
  }
}

Open in new window

0
 

Author Comment

by:Envision11
ID: 35159804
Ok so this is my working code, thanks for the help folks!

window.onload=init; 
function init()
{
  var o = document.getElementsByTagName("div");
  for (var i = 0; i<o.length; i++) {
    if (o[i].className == "videoContent" ) {
        var URL = o[i].getElementsByTagName('iframe')[0];
        alert(URL.src);
        
          
          
        //split URL to get video ID
        //Add Video ID to next button with ID (button)
        //Do this for all 10 buttons on the page
    }
  }
}

Open in new window

0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question