简体   繁体   English

是否可以使用iframe内页中存在的JavaScript更改iframe src?

[英]Is it possible to change iframe src using javascript exists in page inside that iframe?

I want to change the iframe source when something change in this iframe content, the iframe is in a different domian than the parent page. 当此iframe内容发生某些变化时,我想更改iframe源,而iframe与父页面位于不同的domian中。 is this can be done or not? 这可以做到吗?

I just need to add a hash to this iframe src, so that i can access this hash value from the parent page and do some stuff based on this value. 我只需要向此iframe src添加一个哈希即可,这样我就可以从父页面访问此哈希值,并根据此值做一些事情。

What i did: 我做了什么:
In the iframe page i wrote: window.location.hash = "close_child"; 在iframe页面中,我写道: window.location.hash = "close_child";
and in the parent page i wrote: if (document.getElementById("MyIFrameId").src.indexOf("#close_child") > 0) { but i always find that the src is empty 并在父页面中写道: if (document.getElementById("MyIFrameId").src.indexOf("#close_child") > 0) {但我总是发现src为空

I'm not 100% sure, but I think you should be able to do this without a problem. 我不确定100%,但是我认为您应该可以做到这一点。 Since an iframe is just a frame, you should be able to change the location property from within the code inside the iframe (same as you would for anything else), and that change should be visible to the parent page whenever it is made. 由于iframe只是一个框架,因此您应该能够在iframe内的代码内更改location属性(与其他操作相同),并且所做的更改对父页面都是可见的。

Yes, it can be done. 是的,可以做到。 A parent object can NOT see into the iFrame, but the IFrame CAN call a JavaScript procedure from code executing within the iFrame, passing it parameters such as your hash value. 父对象无法看到iFrame,但是IFrame可以从 iFrame中执行的代码中调用JavaScript过程,并向其传递诸如哈希值之类的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM