?
Solved

Get URL from Iframe using Javascript

Posted on 2011-03-16
7
Medium Priority
?
432 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 total points
ID: 35156732
line 7 :  var p = o.getElementsByTagName('iframe')[0];
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
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…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

650 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