简体   繁体   中英

Text find inside a cross-domain iframe

Is it possible i can find and compare some text strings between my website and a cross domain iframe inside it? Suppose i have:

<html>
<body>
<div><p>hello world</p></div>
<iframe><html><body><div><p>hello world</p></div></body></html>
</body>
</html>

is it possible to build a find and compare algorithm that will be able to check the text on my website and compare it with the one inside the cross domain iframe? Should i require the use of the postmessage or is there something simpler in terms of just making just a simple text find and compare inside the browser screen?

A Javascript function running in one frame will not be able to access DOM of other frame if the domains are different. Your need to have the same method loaded in both frame. One approach to do it can be that you load the find/compare JS function in both frames then call it in one frame. Once the key is found in the present frame then via postMessage , send the event to call the same function in the iframe. If in iframe also, you find the same key then you can do postMessage to parent frame that the key has been found.

However if the domains are same then you can access the DOM of an iframe via jQuery like:

var iframeElementContents = $("#yourIFrame").contents();

And now you can run your JS function on this element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM