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{
}
2
  • Please share some code snippet to show what you have tried till now. Commented Jan 17, 2021 at 20:04
  • Here stackoverflow.com/questions/7545008/… Commented Jan 17, 2021 at 20:04

3 Answers 3

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'
  } 
Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately this doesn't work for me. I correct the alert with ')' but it still dont alert me
what do you get when you console.log y.body.textContent
check your variable names and Id's, Share the code snippet
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.

10 Comments

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
What does console.log(iframe_html) prints to console?
It logs "undefined" Could it be a problem, that the iframe source uses <body cz-shortcut-listen="true"> as body tag?
Did you changed the iframe id to frami in document.getElementById('iframe')?
Yes I did. I'm wondering, that NONE solution is working...
|
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

3 Comments

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.
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.