0

It's possible to test for the existence of an HTML element using jQuery. For one example:

It is possible to do the same for an element that is inside an iframe? My best attempt:

if($("#frameId").contents().find('elementName').length > 0)
{
  // do something with the element
}

EDIT: To avoid chasing the cross-domain rabbit, assume that yes, the files are (will be) in the same domain.

Community
  • 1
  • 1
james.garriss
  • 12,959
  • 7
  • 83
  • 96
  • Good question. Both are from the local file system at the moment (while in dev). Eventually the HTML page will be served from a domain and the contents of the iframe will be drag-and-dropped from the local file system. – james.garriss Dec 09 '11 at 15:21
  • For your dev environment you must check the same-origin policy for files specific for your browser. This is the one for mozilla https://developer.mozilla.org/En/Same-origin_policy_for_file%3A_URIs for your domain your pages must be from the same domain. – Martijn B Dec 09 '11 at 15:33

3 Answers3

1

This question has many duplicates on stackoverflow (and possibly all across the internet too). Basically, your iframe must be on the same domain of the the app..

check this post for more details: jQuery/JavaScript: accessing contents of an iframe

Community
  • 1
  • 1
MilkyWayJoe
  • 9,082
  • 2
  • 38
  • 53
0

You can do this only if the i-frame is on the same domain as the main page otherwise this won't work.
If they are on the same domain this will work correctly.

Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
0

Someone posted the right answer, but then deleted it. Don't know why. This is what worked:

if($("#frameId").contents().find('elementName').length)
{
  // do something with the element
}

Thanks, Mystery Man!

james.garriss
  • 12,959
  • 7
  • 83
  • 96