getElementById within iframe

戏子无情 提交于 2019-12-02 02:21:48

问题


Q: I have an iframe calling page X, on page X is a div w/ id=test. The value of this test div is "bubbles". On the parent page I need to read the value of the div and store it as a javascript var.

Outcome: on the parent page have a document.write(iframedivvalue); output that will = whatever the value of the div inside the iframe.

Note:

  • as of right now page X is on a different domain.
  • I am NOT trying to set anything inside the iframe, just read a divs value.

回答1:


You will still be blocked by the same-origin policy if the domains mismatch. Doesn't matter if you're just trying to grab a value.




回答2:


As Alex says on the comment above, you will still be blocked by the JavaScript 'same-origin' policy.

If your iframe is on the same domain, then you could try this:

document.getElementById('iframe-id').contentDocument.getElementById('canvas');



回答3:


Assuming the iFrame has an assigned ID:

var iframe_div = document.getElementById('iframeid').document.getElementById('mydiv');
var content = iframe_div.innerHTML;

I believe should work.



来源:https://stackoverflow.com/questions/5022464/getelementbyid-within-iframe

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