[英]why $(window['iframeName'].document.body).html() doesn't work when i change .attr('src')
Why doesn't 为什么不
$(window['iframeName'].document.body).html()
...work when i change .attr('src')
...当我更改
.attr('src')
When i set the src attribute of iframe to any url when i creating the page, this code 当我在创建页面时将iframe的src属性设置为任何网址时,此代码
$(window['iframeName'].document.body).html()
...will work. ...将工作。
But when i change the src attribute by .attr('src',"www.google.com.sa")
and wait to load the page, 但是,当我通过
.attr('src',"www.google.com.sa")
更改src属性并等待加载页面时,
$(window['iframeName'].document.body).html()
...will doesn't work. ...将不起作用。
Where is the problem ? 问题出在哪儿 ?
尝试:
.attr('src',"http://www.google.com.sa")
A couple of things: 有两件事:
Firstly, setting attr('src') will create a new history item. 首先,设置attr('src')将创建一个新的历史记录项。 You should consider if you actually want to do that, eg if someone can click the back button and get the last page.
您应该考虑是否确实要这样做,例如有人可以单击“后退”按钮并获得最后一页。 If you don't want the changed state in the history (desirable for ads or apps that use iframes instead of outright ajaex), then you would want to change the document's location instead.
如果您不希望更改历史记录的状态(对于使用iframe而不是完全ajaex的广告或应用程序而言是理想的),则可以改用文档的位置。
Secondly, I find it much easier to interact with the page like this: 其次,我发现与这样的页面进行交互要容易得多:
$("#frameID").contents().find("jquery selector");
You can get the full HTML using a call ilke this: 您可以使用以下调用获得完整的HTML:
$("#frameID").contents(document).children().html()
When you load a page from a different domain in the iframe, you can't access it. 从iframe中的其他域加载页面时,您将无法访问它。 This is for security reasons.
这是出于安全原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.