user609306
user609306

Reputation: 1371

How to load cross domain url in html script tag

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.

Upvotes: 2

Views: 4178

Answers (1)

Gabriel Ilharco
Gabriel Ilharco

Reputation: 1679

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>

Upvotes: 1

Related Questions