1

I got this iframe:

<iframe id="frami" src="http://192.168.178.22/file.html"></iframe>

which contains the text:

var m1_enable="1"; var m1_x="0"; var m1_y="570"; var m1_w="1920"; var m1_h="510"; var m1_sensitivity="50"; var m1_threshold="0";

How can I check if m1_enable="1" exists in the iframe?

I tried this now:

iframe_html = document.getElementById('frami').contentWindow.document.body.innerHTML;
if(iframe_html.includes("var")){
    alert();
}
else{
}
AbsoluteBeginner
  • 2,160
  • 3
  • 11
  • 21
  • Please share some code snippet to show what you have tried till now. – sonali Jan 17 '21 at 20:04
  • Here https://stackoverflow.com/questions/7545008/how-to-obtain-entire-text-content-of-a-web-page-within-a-iframe-using-javascript – lissettdm Jan 17 '21 at 20:04

3 Answers3

1

If your iframe is on the same domain it is document.getElementById('frami').contentWindow['m1_enable'] (if this isn't null, compare with 1 with ==1). otherwise, look at window.postMessage

https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

  • Can you clarify, are you actually looking for text in the body of the document? It looks like you are trying to check the value if a javascript variable. – user1499210 Jan 17 '21 at 20:22
  • The iframe i include is the return of a cgi response. I want to change the color of a button if m1_enable=1 to green and if not to red. – Dominik Weber Jan 17 '21 at 20:40
  • I would try var btnColour =document.getElementById('frami').contentWindow['m1_enable'] ; if(btnColour !=null && btnColour == 1) {//set to green}else{//set to red} - sorry about the formatting, typing in phone. Using ==1 will return true if it is either a string or an integer. – user1499210 Jan 17 '21 at 22:27
1

iframe tag has a property called contentDocument that returns the document object generated by an iframe and another called contentWindow that returns the window object generated by an iframe. You can check if "m1_enable="1" by

var x = document.getElementById("frami");
  var y = (x.contentWindow || x.contentDocument);
  if (y.document) y = y.document;
  if (y.body.textContent.indexOf('m1_enable="1"')>-1){
    alert('found the phrase'
  } 
sonali
  • 762
  • 10
  • 23
1

You can get the HTML from iframe with:

iframe_html = document.getElementById('iframe').contentWindow.document.body.innerHTML

And check if it includes m1_enable="1" with:

iframe_html.includes("m1_enable='1'")

This will work only if the frame source is on the same domain. If it is from a different domain, cross-site-scripting (XSS) protection will kick in.

iamdlm
  • 1,885
  • 1
  • 11
  • 21
  • I use now: `iframe_html = document.getElementById('frami').contentWindow.document.body.innerHTML; if(iframe_html.includes("m1_enable='1'")){ alert(); }` but I dont get an alert... I'm on the same domain though – Dominik Weber Jan 17 '21 at 20:17
  • What does `console.log(iframe_html)` prints to console? – iamdlm Jan 17 '21 at 20:23
  • It logs "undefined" Could it be a problem, that the iframe source uses as body tag? – Dominik Weber Jan 17 '21 at 20:28
  • Did you changed the iframe id to `frami` in `document.getElementById('iframe')`? – iamdlm Jan 17 '21 at 20:34
  • Yes I did. I'm wondering, that NONE solution is working... – Dominik Weber Jan 17 '21 at 20:38
  • Is the iframe in HTML or is loaded with JavaScript? Maybe when you trying to access the iframe is not rendered yet. – iamdlm Jan 17 '21 at 20:51
  • It is loaded before the javascript-script which is embedded with " – Dominik Weber Jan 17 '21 at 20:54
  • Can you share the full JS code? The solution I gave is working on my side and if the iframe is loaded via JS it's probably related with synchronism. – iamdlm Jan 17 '21 at 20:56
  • This is the whole code i use ` ` – Dominik Weber Jan 17 '21 at 20:58
  • [This is working](https://jsfiddle.net/ax0hf4s1/). You said above that the iframe is the return of a cgi response, what does that mean? – iamdlm Jan 17 '21 at 21:06