Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi!

I am embedding an iframe which has src ="" from another domain.

For example: my page is www.abc.com/Default.aspx, and the iframe
HTML
<iframe id="myFrame" src="www.xyz.com/search.aspx"></iframe>
.
All that I want to do is when the iframe loaded, I will navigate throught the DOM and select a specific div and change the content of that div like:
JavaScript
$(function(){
    $("#myFrame").load(function(){
         var iBody = $("#myFrame").contents().find("body");
         var iDiv = iBody.find("#divID");
         /*code to change the div content*/
    });    
});


However, I cannot access the iframe DOM since my iframe is crossed- domain iframe, and the access is denied for security reason.

Can anyone give me a solution? I am using .NET C# for my project.
Posted
Updated 15-Jul-12 20:44pm
v2
Comments
StianSandberg 16-Jul-12 1:46am    
This is not possible as far as I know. It's a security issue. If you want to modify the dom you are stuck, but if you want to just read data from the page, then you could do that by using a webclient etc (serverside) http://www.codeproject.com/Articles/33798/HTTP-GET-with-NET-WebClient
Kenneth Haugland 16-Jul-12 1:54am    
IT is possible, but its quite a hack
namhung1986 16-Jul-12 2:37am    
Frankly, I am embedding an iframe which is a search tool for flight schedule from a 3rd party company in my page (Default.aspx). When I fill in search criterias and click "Search", the result of flight schedule appears in the same page Default.aspx. It's good! However, after that, if I choose a flight and click "Book", it will navigate to the 3rd company's website. Therefore, I want to change its action.

I want to use the 3rd party service to display the result only, and use Jquery to navigate throught iframe DOM to insert my own "Book" button so that when the user click "Book", it will save the flight to my database, not the 3rd party database.

1 solution

A few possible solutions:

  • Ask this 3rd party company for an api.
  • Download the whole site (with webclient etc) and serve it from you own domain. Then this security issue is gone.
    You can even download using Chilkat MHT .NET Component so all style, script, and images come along. Then you can even modify it server side..

Any other suggestions? Kenneth?[^] :)
 
Share this answer
 
Comments
enhzflep 16-Jul-12 4:27am    
Somehow doubt that a(ny) company will allow it's online booking system to be cloned! It will certainly be non-functional..
StianSandberg 16-Jul-12 5:40am    
I agree. And it's the same issue using an iframe an modify the DOM. So i guess the best way to do this is to ask for an api
namhung1986 17-Jul-12 1:24am    
http://whitelabel.dohop.com/w/ThangPham/?a1=SGN&a2=HAN&return=1&d1=010812&d2=050812

Can anyone get the result table of this site? I was trying to use WebRequest, WebResponse to do that, but I got failed.

string root = "http://whitelabel.dohop.com/w/ThangPham/?a1=SGN&a2=HAN&return=1&d1=010812&d2=050812";
WebRequest request = WebRequest.Create(root);
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
var dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
lblResult.Text = responseFromServer;
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900