S
S
skyfly20102015-10-26 01:16:47
JavaScript
skyfly2010, 2015-10-26 01:16:47

How to organize iframe parsing using javascript?

Good night, dear friends, there was a need to parse iframe content. You can view the source code of the document in the iframe through any browser, but something has not yet been parsed. I'm sorry that maybe many of my codes are not correct, I just started learning javascript.
So now we have:

var iframe = document.getElementById('happyFrame'); 
var frameDocs = iframe.contentDocument || iframe.contentWindow.document;
var link = frameDocs.getElementsByClassName('name');
alert(link.length);

In this case, alert outputs 0. While the link should be in it.
The class named 'name'. there is only one in the iframe code.
An example of a link whose backbone you need to get:
<div class="item_info">
 <a href="http://yyxxhh.com/u1jj12232lj" class="name" target="_blank">Мялка-палка</a>
</div>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mark Doe, 2015-10-26
@skyfly2010

As iframe.contentDocument documentation says
Those. this property is available only for frames that are on the same domain as the current document (for security purposes, for example, so that you don’t load social network messages in the frame and parse them)
However, if you want and really, really need to, you can do cross-domain ajax- request for the address of the frame using YQL (how exactly, you can learn more in this article), and it’s convenient to parse the necessary information.
Here, for example, a quick sketched example - parses the name of the last added character from a frame on a foreign domain and, accordingly, a picture with it output.jsbin .com/todetucede

A
Alexey Blyshko, 2015-10-26
@nekt

If the iframe contains content from another domain, then getting access to it from the outside is quite problematic - there are quite evil security settings.
An example of an article describing them habrahabr.ru/company/mailru/blog/210050
In general, it is easier to parse documents on the server side.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question