简体   繁体   English

如何在不将iframe放在页面上的情况下获取iframe的内容?

[英]How can I get the contents of an iframe without putting the iframe on the page?

How can I get the html of an iframe without physically putting the iframe on the page? 如何在不将iframe实际放在页面上的情况下获取iframe的html? Can I put the iframe into a variable and get it that way? 我可以将iframe放入变量中并以这种方式获取它吗?

If you're using node.js (which I would recommend for this), then take a look at jsdom . 如果您使用的是node.js(我会为此推荐),那么请看一下jsdom

As for javascript specifically, you cannot make a call to an external page using javascript (without using hacky methods), as it would violate Cross-domain policy 对于javascript,您不能使用javascript调用外部页面(不使用hacky方法),因为这会违反跨域策略

An untested afterthought, but you might be able to put an iframe hidden through css and then access the content through the document/jquery. 未经测试的事后想法,但您可以通过CSS将iframe隐藏起来,然后通过文档/ jquery访问内容。

You can make a php-page with the HTML-content, Easily include the file with php include: 您可以使用HTML内容制作一个php页面,使用php include轻松包含该文件:

<?php
    include "filename.php";
?>

but...why? 但为什么?

You cannot use javascript to get content from cross domain url. 您无法使用javascript从跨域网址获取内容。 So even if you have actual iframe element on your page, you will not be able to access its elements using javascript. 因此,即使页面上有实际的iframe元素,您也将无法使用javascript访问其元素。 This is called Same Origin Policy . 这称为“ 同源策略”

though JSONP is an alternative to your case, you will not be able to access elements inside iframe. 尽管JSONP可以替代您的情况,但是您将无法访问iframe中的元素。 You can use Jquery and JSONP to get html of cross domain page 您可以使用Jquery和JSONP获取跨域页面的html

Another option For this scenario is, you can grab the content from server side scripting language like c#, PHP 对于这种情况,另一个选择是,您可以从服务器端脚本语言(如c#,PHP)中获取内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM