Alešinar
Alešinar

Reputation: 123

Get innerText from iframe from parent

Hi I'm adding a number of iframes to my page. Iframes contain only one value and I would like to get it form the parent. I tried many of the answers posted here on the stack overflow but none of them works for me. Below I attached the image so you can see how is the iframe included in the page. I would like to get the value in the red frame so I can use it later.

enter image description here

The code:

  var graphUrls = tmpSource.getGetFeatureInfoUrl(
    graphCoor, viewResolution, 'EPSG:3857', {'INFO_FORMAT': 'text/html'});
  var frr = document.createElement("iframe");
  frr.onload = function(){
    var jjj = $("iframe body a").text()
    var kkk = $("iframe body #ras-val").text()
    console.log("jjj ", jjj)
    console.log("kkk ", kkk)
  };
  frr.id = sourceParams.year + "-" + sourceParams.month + "-" + sourceParams.day;        
  frr.src = graphUrls
  document.body.appendChild(frr);

Upvotes: 0

Views: 2007

Answers (1)

Kaiido
Kaiido

Reputation: 136618

If the returned url is from the same domain as your main document, and only in this case,
then you can pass the contentDocument of your iframe as the second parameter of $(selector, context).

// frame is you iframeElement
$('#ras-val', frame.contentDocument).text()

As a fiddle since StackSnippet's null origined frames won't allow us to access inner frames...

Note that the selector will then be relative to this context, so you won't pass "iframe body a" but just "body a".

And in case the url is not from the same domain, then your front-end code is not allowed to access this document for security reasons.

Upvotes: 2

Related Questions