Select frame context with javascript

房东的猫 提交于 2020-01-03 04:11:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!