2

I want to load cross domain url html content on my webpage. I read that it could be achieved by loading url in script and calling global callback function. My code is below-

<html>
<body>

<div id="response"></div>

<script>
  function myFunction (data) {
    var stringData = JSON.stringify(data)
    document.getElementById('response').innerHTML = stringData;
  }
</script>
<script src="http://zeenews.india.com?callback=myFunction()"></script>

</body>
</html>

When i run this HTML file, i get below error

Uncaught SyntaxError: Unexpected token <

I could also see this url in front of error

?callback=myFunction():2 

Please help me.

user609306
  • 1,333
  • 6
  • 17
  • 27
  • I'm guessing `zeenews.india.com` isn't actually a script file, but HTML – adeneo Jan 12 '16 at 17:42
  • Yes, I want to load HTML file. – user609306 Jan 12 '16 at 17:43
  • You can't do that in a script tag, if you want the HTML you'll have to use the serverside to scrape it – adeneo Jan 12 '16 at 17:43
  • but they told on SO that it can be done this way – user609306 Jan 12 '16 at 17:44
  • Only if it's a **javascript** file, not HTML – adeneo Jan 12 '16 at 17:44
  • On this URL http://stackoverflow.com/questions/6046008/jsonp-request-returning-error-uncaught-syntaxerror-unexpected-token, he writes "There is a workaround. script tags are not subject to this restriction. This means that you can insert a script tag into the document that calls a URL. If you define a globally-accessible function in your script and tell the remote server what that function is called, the server can pass code that wraps the data to be sent in a call to that function." – user609306 Jan 12 '16 at 17:46
  • And that answer is meant to be about javascript files, where one could use a valid javascript file containing a function wrapping the data to be returned, and then insert that **valid** javascript file dynamically, call the callback function, and get the data. It **does not** work with just any old websites HTML. – adeneo Jan 12 '16 at 17:55
  • ok. Is there any way on this earth to get cross domain HTML content from client side under all circumstances? Thanks – user609306 Jan 12 '16 at 17:56
  • No ............ you have to use a serverside script, which you *then* can call from the clientside – adeneo Jan 12 '16 at 17:57
  • Well, you could make `#response` into an iFrame, that would display the site, but you still wouldn't be able to access the HTML. – adeneo Jan 12 '16 at 17:58
  • I believe what you want to use is the ` – Gabriel Ilharco Jan 12 '16 at 18:17

1 Answers1

1

You are getting this error because of this tag

<script src="http://zeenews.india.com?callback=myFunction()"></script>

, since the url you entered doesn't return a javascript file. The src attribute in the <script> tag is for js only.

If you want to display the content of the webpage inside the response div tag, you should use the <iframe> tag inside that div, like this:

<div id="response">
    <iframe src="http://zeenews.india.com"></iframe>
</div>
Gabriel Ilharco
  • 1,649
  • 1
  • 21
  • 34