2

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

1 Answer 1

0

Same domain is easy:

 window.parent.doSomeThing()

Cross origin is a little tricky

window.postMessage allows for sending data messages between two windows/frames across domains

Caniuse.com lists the browsers already supporting cross-document (postMessage).

Sign up to request clarification or add additional context in comments.

2 Comments

Hi @VDP. Thanks for your help. I am trying to use postMessage, but doing otherWindow.addEventListener("message", receiveMessage, false); results in security error because of cross origin. This defies the purpose of using postMessage API in the first place because that's precisely what I need. Do you have any advice?
My bet is you're using IE < IE11 :( It seems it's a known issue... Here's another stackoverflow question about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.