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.

11
  • I'm guessing zeenews.india.com isn't actually a script file, but HTML Commented Jan 12, 2016 at 17:42
  • Yes, I want to load HTML file. Commented Jan 12, 2016 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 Commented Jan 12, 2016 at 17:43
  • but they told on SO that it can be done this way Commented Jan 12, 2016 at 17:44
  • Only if it's a javascript file, not HTML Commented Jan 12, 2016 at 17:44

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

is there anyway to get cross domain HTML through script tag? they talked about it on SO at many places
Ajax is subject to the same-origin policy, so that probably doesn't work at all.
@user609306, no there is not. Why would you even want to do something like that? Script tags are for scripts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.