好吧,我有一个IFrame,它调用相同的域页面.我的问题是我想从这个被调用的页面(来自JavaScript)访问来自这个父iframe的一些信息.如何访问此iframe?
细节:有几个像这样的Iframe,可以加载相同的页面,因为我正在编写Windows环境.我打算关闭这个Iframe,这就是为什么我需要知道我应该从他内部关闭的原因.我有一个数组保持对这些Iframe的引用.
编辑:动态生成iframe
我正在写一个简单的网站,作为一个成语输入,并从牛津词典返回其含义和例子.这是我的想法:
我向以下网址发送请求:
http://www.oxfordlearnersdictionaries.com/search/english/direct/?q=[idiom]
Run Code Online (Sandbox Code Playgroud)
例如,如果成语"不太远",我会发送一个请求:
http://www.oxfordlearnersdictionaries.com/search/english/direct/?q=not+go+far
Run Code Online (Sandbox Code Playgroud)
我将被重定向到以下页面:
http://www.oxfordlearnersdictionaries.com/definition/english/far_1#far_1__192
Run Code Online (Sandbox Code Playgroud)
在这个页面上,我可以提取成语的含义和例子.这是我的测试代码.它会提醒响应网址:
<input id="idiom" type="text" name="" value="" placeholder="Enter your idiom here">
<br>
<button id="submit" type="">Submit</button>
<script type="text/javascript">
$(document).ready(function(){
$("#submit").bind('click',function(){
var idiom=$("#idiom").val();
$.ajax({
type: "GET",
url: 'http://www.oxfordlearnersdictionaries.com/search/english/direct/',
data:{q:idiom},
async:true,
crossDomain:true,
success: function(data, status, xhr) {
alert(xhr.getResponseHeader('Location'));
}
});
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
问题是我有一个错误:
跨源请求已阻止:同源策略不允许在http://www.oxfordlearnersdictionaries.com/search/english/direct/?q=by+far上读取远程资源.这可以通过将资源移动到同一域或启用CORS来解决.
谁能告诉我如何解决这个问题?另一种方法也很好
我控制嵌入在另一个域的页面中的iframe的内容.我的iframe中的javascript是否有任何方法可以更改父级的DOM?
例如,我想让我的iframed脚本向父DOM添加一堆html元素.这看起来像一个很高的命令 - 想法?
编辑:存在一种称为" 片段ID消息 "的技术,它可能是跨域iframe之间进行通信的一种方式.
编辑:此外,Firefox 3.5,Opera,Chrome(等)似乎采用html5 "postMessage"api,它允许帧,iframe和弹出窗口之间的安全,跨域数据传输.它就像一个事件系统.IE8显然支持这个功能,这可能有点令人惊讶.
摘要:不,您无法直接从其他域访问/编辑页面的DOM.但是你可以与它进行交流,它可以合作进行你想要的改变.
当iframe中加载的页面来自另一个域时,我们如何从iframe中加载的文档中访问父文档?
我收到了一个权限被拒绝的错误.
我在旧网站上的asp页面的iframe中运行了一个c#.net应用程序.访问Asp页面的会话信息有点困难,所以我想让我的.net应用程序只是验证它是从批准的页面调用,否则立即停止.
有没有办法让页面找到它的父文档的URL?
我正在尝试动态调整iframe的大小以适应其内容.为此,我有一段代码:
$("#IframeId").height($("#IframeId").contents().find("html").height());?
Run Code Online (Sandbox Code Playgroud)
它不起作用.是因为跨域问题吗?我如何使它适合?请看看Fiddle:JsFiddle
ps我已经设置了链接的html和正文 height:100%;
在fancybox窗口中打开了一个iframe.
iframe的内容是外部的.
有没有办法访问此内容并更改其中一个元素的样式?
我有一个带有iframe的页面,在这个iframe的内容中,所有窗口都有重定向,我该怎么做?
尝试:
window.location.href = 'logged.html';
如果页面是在框架中打开还是向上打开,我可以得到?
if(isiframe) window.top.location.href
Run Code Online (Sandbox Code Playgroud)
---解决方案---
if($("#cboxOverlay", top.document).length > 0)
if($("#cboxOverlay", top.document).css("display") != 'none')
window.top.location.href = '{{ path('portada') }}';
Run Code Online (Sandbox Code Playgroud) 我正在学习建立Chrome扩展,而我试图按照官方指南中的指令之一在这里.
我想要完成的是:
到现在为止还挺好!我使用以下脚本注入页面.
var injectJS = function(url, cb) {
var h = document.getElementsByTagName('head')[0],
s = document.createElement('script');
s.type = 'text/javascript';
s.src = url;
if (cb)
s.onload = cb;
h.appendChild(s);
};
injectJS(chrome.extension.getURL('script/do_something.js'));
Run Code Online (Sandbox Code Playgroud)
现在,我希望注入的脚本能够与扩展进行通信.
看起来我正在寻找的是文档中描述的内容.
https://developer.chrome.com/extensions/content_scripts.html#host-page-communication
问题是,当我尝试执行window.postMessage控制台时显示错误"DOM Exception 12".
编辑:解决了运行示例代码的第一个问题.
我得到的另一个错误,来自smae代码来自port.postMessage:
Uncaught Error: Attempting to use a disconnected port object
这是代码:
var port = chrome.runtime.connect();
// Respond to messages from the injected script to collect results
window.addEventListener('message', function(e) {
if (e.source != window)
return; …Run Code Online (Sandbox Code Playgroud) iframe ×6
javascript ×6
cross-domain ×3
jquery ×3
ajax ×1
dom ×1
fancybox ×1
html ×1
redirect ×1