以编程方式创建IFrame并返回其location.search

JsR*_*ico 4 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
}
Run Code Online (Sandbox Code Playgroud)

won*_*ng2 6

我试过这个:

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");
Run Code Online (Sandbox Code Playgroud)

但Chrome表示您无法访问具有不同域的帧.(域,协议和端口必须匹配.)