Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript element tag name undefined

So I'm not that great with Javascript so I'll put that forward right away. That being said, I've looked up as much as I could on this particular problem before asking, but the suggestions haven't solved my issues. I'm ultimately trying to pull all of the links from an iframe window on the same domain as the main page. Then I want to basically search that link array to match it with the current page to trigger a CSS modification to the html code (this part is not coded yet, FYI). So here is the part I have so far: Side note: The confirms are in there to debug the code and try to tell me where it's failing and what my queries are returning, they won't stay obviously when this is finished. I appreciate any advice that may help me fix this!

<script type="text/javascript">
// main is the iframe that I'm trying to search for a tags
document.getElementById("main").onload = function() {
	confirm("test");
	var main = document.getElementById("main");
	var anchors = main.contentWindow.document.getElementsByTagName('a');
	confirm(anchors[1]);
	for (var i in anchors) {
		confirm(anchors[i].getAttribute("href"));
	}
};
</script>
like image 782
Matthew Walker Avatar asked Mar 08 '26 09:03

Matthew Walker


1 Answers

I have created a plunker for you its working. I think its the placement of code in your file is causing the problem.

<iframe id="main" src="content_if.html"></iframe> 
 <script>
   // main is the iframe that I'm trying to search for a tags
  document.getElementById("main").onload = function() {
    confirm("test");
    var main = document.getElementById("main");
    var anchors = main.contentWindow.document.getElementsByTagName('a');
    confirm(anchors[1]);
    for (var i in anchors) {
        confirm(anchors[i].getAttribute("href"));
    }
  };

 </script>

You should use jQuery to do this in a cross browser way. Include jQuery in page

<script src="https://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

and follow this post

like image 168
Saqueib Avatar answered Mar 09 '26 22:03

Saqueib