javascript - 以编程方式创建 IFrame 并返回其位置。搜索

标签 javascript iframe

我需要一个接受 URL 并将该 URL 处的 iframe 附加到 iframe 的父文档正文的函数。然后该函数应返回该 iframe 的 window.location.search,而无需知道为该函数提供的 URL 参数。

我如何在 JavaScript 中执行此操作?

编辑:如果第一句话对您没有意义,请试试这个:我需要一个将 iframe 附加到文档正文的函数,给定一个 URL 作为参数。换句话说:

function appendIFrame(url) {
    // create iframe whose address is `url`
    // append iframe to body of document
    // return location.search of iframe's `window`, without using `url` argument
}

最佳答案

我试过这个:

function foo(url) {
    var iframe = document.createElement("iframe");
    iframe.src = url;
    iframe.name = "frame"
    document.body.appendChild(iframe);
    return frames["frame"].location.host;
}
foo("http://google.com");

但 Chrome 表示您无法访问具有不同域的框架。(域、协议(protocol)和端口必须匹配。)

关于javascript - 以编程方式创建 IFrame 并返回其位置。搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5440518/

相关文章:

c# - 如何使用 javascript 在 asp.net 中的 gridview 中单击按钮时应用文本框空白验证?

Javascript:function.prototype.bind() 中的参数解包?

javascript - 多个选项卡的 R Shiny 页面刷新按钮

javascript - IFrame 中 ExtJS 5 应用程序的滚动问题

javascript - 在 iFrame 中禁用外部内容加载

javascript - Angular.js 和 HTML5 日期输入值——如何让 Firefox 在日期输入中显示可读的日期值?

javascript - ClosureCompiler 通过高级优化删除死代码

php - 在外部网站上完成预订后检索信息(cURL、iFrame...?)

javascript - 使用 Javascript 上传文件返回 'Access Denied' 错误与程式化 <输入类型 ='file'> 到按钮

javascript - 在 Iframe 中编辑博客数据