0
<iframe  id="frame" style=" width:100%; height: 700px;    margin:30px 0 0 0px;
                         border-style: none; " src="" ></iframe>
 <button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    var x = document.getElementById("frame");
    var y =  x.contentDocument;
    if (y.document)y = y.document;
    y.body.style.backgroundColor = "red";
   //  alert("c");
     //   window.alert(5 + 6);
        x.src="some_link";
//document.write("k");
var xx = document.getElementsByClassName('nav-link');
//alert(xx);
var j = 0;
while (xx[j]){
xx[j].setAttribute('href', '');
alert("h"+j);
j++;
}
}

</script>
    var xx = document.getElementsByClassName('nav-link');
    There is error in this line i guess.
Not able to retrieve the links.
  • 3
    Possible duplicate of [How to get the href link from iframe](https://stackoverflow.com/questions/16906868/how-to-get-the-href-link-from-iframe) – Derek Ribeiro Feb 04 '18 at 05:10

2 Answers2

0

How to get the href link from iframe

Here is a example using Jquery, but you can do that using pure javascript.

After all that, you can use the Jquery property, length. It's easier.

Derek Ribeiro
  • 108
  • 1
  • 8
0

You're using the parent document

var xx = document.getElementsByClassName('nav-link');
         ^

Instead, use the y variable which has the frame's document

 var y =  x.contentDocument;
            ^

 if (y.document)y = y.document;
                      ^

Assign to y the right document object

y = x.contentWindow.document

Use the following line of code:

var xx = y.getElementsByClassName('nav-link');
Ele
  • 33,468
  • 7
  • 37
  • 75
  • getElementsByClassName is no such property of y – upasana arora Feb 04 '18 at 04:59
  • Look the updated answer. Assign `y = x.contentWindow.document` in order to get the right document object. – Ele Feb 04 '18 at 05:03
  • If i replace the some_link with the https://vjudge.net/contest/209723. I am not able to get the links.What is the error, can you help.The html collection is of lenghth zero.why it is not collecting the links – upasana arora Feb 04 '18 at 05:28