ChUck_PrOg
ChUck_PrOg

Reputation: 481

Jquery search text in iframe

Can anyone help?

i want to search a string inside an iframe like for example. i have a page called main.html which has an iframe and the contents of the iframe(somesite.com) has a string inside lets say "Home". what i want to happen is to have a script that will search the string "home" inside the iframe "#com" and will alert a message stating the string is found. i want this to be done with jquery and not with server side scripting. please note that somesite.com is an external site and i have no control over it.

main.html:

<html>
<body>
<input type="button" id="search" value="search">
<iframe id="com" src="http://somesite.com"></iframe>
</body>
</html>

somesite.com

<html>
<body>
Home
</body>
</html>

sorry for the bad english, just comment if you have some clarifications.

Upvotes: 3

Views: 6367

Answers (2)

talha2k
talha2k

Reputation: 25473

Something like this may help:

var text = $('#com').contents().find('body').html();
alert(text);

OR

$("body", $("#com").contents()).each(function(){
  if ($(this).text() == "Home") {
    alert('content found');
  }
});

Upvotes: 0

Bart Platak
Bart Platak

Reputation: 4475

You could try:

$("#com").contents();

Note that $.contents() can only be performed on an iframe that originates from the same domain due to Same Origin Policy.

So that you could use it to display alert:

if($("#com").contents().text()search("someText")!=-1){
    alert("found");
}

Upvotes: 6

Related Questions