0

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.

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.

i have tried various solution like this :

How to get the body's content of an iframe in Javascript?

Community
  • 1
  • 1

1 Answers1

0

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). 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. 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. 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:

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:

$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. 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.

Community
  • 1
  • 1
Aviv Carmi
  • 907
  • 8
  • 21
  • thanks for the reply, my iframe and page are being hosted on the same domain, not sure why i can not perform the required operation when i am doing it correct i suppose http://www.codeproject.com/Questions/865460/accessing-dynamically-created-iframe-contents-elem?arn=0 – Rajneesh Master Jan 16 '15 at 04:51