?
Solved

Get URL from Iframe using Javascript

Posted on 2011-03-16
7
Medium Priority
?
435 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 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 35156732
line 7 :  var p = o.getElementsByTagName('iframe')[0];
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 83

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 83

Assisted Solution

by:leakim971
leakim971 earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
Suggested Courses

807 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