【发布时间】:2017-01-31 12:34:38
【问题描述】:
我为我的网站设计了书签。因此,使用 iframe 创建的小书签。 iframe 和 iframe 源在脚本中声明。
我已经为我的源页面设置了关闭按钮并声明了回调函数,但它不起作用。
点击关闭按钮
显示以下错误。
(DOMException: Blocked a frame with origin "http://localhost:8050" from access cross-origin frame. at HTMLButtonElement.onclick (http://localhost:8050/researcher_voice/iframe.html:19:172))。
书签:
<a class="article-bookmark" href="javascript: (function(){
var iframeSrc = 'http://localhost:8050/researcher_20voice/iframe.html';
var iframeStyle = 'position: fixed; z-index: 999999; width: 500px; height: 300px; left:300px; right: 0; top: 200px; border: none; overflow: hidden; background-color: black';
var iframe = document.createElement('iframe');
iframe.setAttribute('src', iframeSrc);
iframe.setAttribute('id', 'kew-modal');
iframe.setAttribute('style', iframeStyle);
var body = document.getElementsByTagName('body')[0];
body.appendChild(iframe);
}).call(this);">Bookmarklet</a>
关闭按钮:
function closeIframe() {
var iframe = document.getElementById('id');
iframe.parentNode.removeChild(iframe);
}
【问题讨论】:
-
如果内容来自同一个来源,你可以 - 但你需要使用
document.getElementById('kew-modal')而不是 ("id") - 因为它可能不是来自同一个来源,你将被禁止 -
而且我也使用过 document.getElementById('kew-modal') 但它重复了相同的跨域错误。
-
所以答案是否定的
标签: javascript