0

I want to use javascript to open one text file in child window, and then read the content to the parent window. How to implement it?

The code like below, if the data.xml is not HTML page, how to get the content to the parent window through javascript?

function op() {
    win = window.open("http://xxx.bb.com/data.xml", "win", "width=200,height=200")
}
bfavaretto
  • 71,580
  • 16
  • 111
  • 150
Robin Wang
  • 779
  • 1
  • 8
  • 16

1 Answers1

0

You have already tried XmlHttpRequest and fallen foul of the cross-domain restriction. A web page (and code contained in it) can only manipulate data or elements of another if both pages are in the same domain. You will run into the same cross-domain restriction with two windows.

The correct method is to use XmlHttpRequest but ensure that the target of that request is in your domain. That will probably involve the creation of a proxy script on your server which can serve pages or data from other domains. Your page requests your script to get the external data; the script gets the data and serves it. Because the script in your domain, the data appears to come from your domain and it's not subject to cross-domain restrictions.

Simple PHP proxy script

If the data you are attempting to get at is not yours, you should really ask permission to deal with it or republish it. Are web developers allowed to scrape html content? (The accepted answer is not the best answer)

Community
  • 1
  • 1
Andrew Leach
  • 12,945
  • 1
  • 40
  • 47