Solved

Get URL from Iframe using Javascript

Posted on 2011-03-16
7
403 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
Comment Utility
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
Comment Utility
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
Comment Utility
line 7 :  var p = o.getElementsByTagName('iframe')[0];
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 82

Expert Comment

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

Author Comment

by:Envision11
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now