Can I tell if a frame access will be cross-domain before performing it?
I am trying to find the DOM iframe
or frame
element for each of the frames in a window's frames
collection. The fastest way to do this, I think, is to access the frame's frameElement
property. This property is only available for a same-domain access, and returns undefined and prints an error message to the log in the cross-domain case.
I can loop through the elements returned from document.getElementsByTagName
to find the correct element in the cross-domain case, but I开发者_如何学运维'd like to avoid printing the "Unsafe JavaScript attempt" message to the log.
Is there any way to tell before trying it if a property access attempt will run afoul of the cross-domain security policy?
Thanks.
You could check the domain on the url of the hosting page and the frame (src property). If the domains differ, you will most likely get an error.
I say most likely because if the frame is on a subdomain, but has specified document.domain
to be the same as the parent frame, then cross-domain access will work.
精彩评论