问题
Is there a way to programmatically select frame context with Javascript? Let's say there are two different frames present in the current web page, and I need to append 'hello' to the second frame. The problem is that the second frame has a different domain than the current web page. With chrome developer tools, I can simply choose the second frame context and do $('body').append('hello') from console, but I should be able to do this programatically.
Reference: Debugging iframes with Chrome developer tools
Is there a way to achieve this?
intention: I need to create a bookmarklet that specifically targets the frame that has a different domain than the main webpage.
回答1:
Same domain is easy:
window.parent.doSomeThing()
Cross origin is a little tricky
here is a good article about it.
If html5 is an option: You can use the postMessage API
Most browsers already have support now:
Here you can find out if the browsers you target are supported.
来源:https://stackoverflow.com/questions/22479117/select-frame-context-with-javascript