简体   繁体   English

从javascript访问动态创建的iframe内容/元素,例如文本框,标签

[英]accessing dynamically created iframe contents/elements like textbox, label from javascript

i am dynamically creating an iframe then calling aspx pages into it, what i need to do is access the elements of the iframe page and change its ( only specific element like text box or label etc.) value without reloading the whole page. 我正在动态创建一个iframe,然后在其中调用aspx页面,我需要做的是访问iframe页面的元素并更改其(仅特定元素,如文本框或标签等)值,而无需重新加载整个页面。

the first task is to access the elements of pages being called into my iframe, i am trying to acess them with javascript but no progress so far. 第一个任务是访问被调用到我的iframe中的页面元素,我正在尝试使用javascript访问它们,但到目前为止没有任何进展。

i have tried various solution like this : 我已经尝试过各种解决方案,例如:

How to get the body's content of an iframe in Javascript? 如何在Javascript中获取iframe的内容?

Actually, the answer you've attached should work. 实际上,您所附加的答案应该可以。 But note that this is only true in case that your parent page and the iframe URL are loaded from the same host (domain name). 但是请注意,仅当您的父页面和iframe URL是从同一主机(域名)加载的情况下,这才是正确的。 if not, you will get an error message from your browser stating that this operation is blocked. 如果不是,您将从浏览器中收到一条错误消息,指出此操作已被阻止。

If you are trying to show another site through and iframe and then manipulate it then you have to give up this dream because it can't happen that simply. 如果您试图通过iframe展示另一个网站,然后对其进行操作,那么您就必须放弃这个梦想,因为它不可能简单地实现。 I can think of one solution for you, not sure about the legality of it, and it is kind of a pain in the ass. 我可以为您想到一个解决方案,不确定该解决方案的合法性,这有点麻烦。

You can open up a server side script on your own domain that receive a URL, fetches it's content and then echo it. 您可以在自己的域上打开一个接收URL的服务器端脚本,获取其内容然后回显它。 This way you get the original desired page contents but you have it on your own host so you can manipulate it as mention in the attached answer . 这样,您可以获得所需的原始页面内容,但是将其放置在自己的主机上,因此可以按照所附答案中的说明进行操作

Note that it's not easy at all to control from there, because once a user clicks a link in the page his out of your control again, so you may want to change all the page links to the address of your server side script and attach the original link to let it fetch it for you. 请注意,从那里进行控制绝对不容易,因为一旦用户单击页面中的链接,他又又失去了控制,因此您可能希望将所有页面链接更改为服务器端脚本的地址并附加原始链接,让它为您获取。 Probably a lot more issues that i haven't thought about. 可能还有更多我未曾考虑过的问题。

PHP Example of such a function: PHP此类功能的示例:

function fetchURL() {
    $urlToFetch = urldecode($_GET['url']);
    $contents = file_get_contents($urlToFetch);
    // maybe here manipulate links and other stuff throw str_replace or,
    // if you want more control over it, you may want to load it in to some DOM parser class,
    // manipulate it and extract the result back to a string variable.
    echo $contents;
} 

Note that in that case you should load the script through the iframe with the desired URL as a query string like that: 请注意,在这种情况下,您应该使用所需的URL作为查询字符串通过iframe加载脚本,如下所示:

$yourDesiredURL = 'http://www.example.com';
echo '<iframe src="http://www.yourdomain.com/your/script/path.php?url=' . urlencode($yourDesiredURL) . '"></iframe>';

*************** EDIT ***************** ***************编辑******************

Actually now i see that you tagged .NET, so my example code is probably not the best for you, but since it's very short and simple it wouldn't be any problem converting it. 实际上,现在我看到您标记了.NET,因此我的示例代码可能不是最适合您的示例,但是由于它非常简短,因此转换它不会有任何问题。 Again, i want to say that iv'e never tried it and it's probably over your (and my) head, maybe you better give up on the idea. 再次,我想说我从未尝试过,它可能在您(和我)的头上,也许您最好放弃这个主意。

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

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