0

im trying to load a specific div from a different webpage.

<div id="result">Loading</div>

<script>
        $( "#result" ).load("http://bte.gep.msess.gov.pt/pesquisa_avancada.php #boxinterior" );
        </script>

I already tried with jquery and with javascript but its not working properly, as it should. Any ideias? The #boxinterior is the div from the other website.

EDIT

its not showing me the div as it should and its giving me a

XMLHttpRequest cannot load http://bte.gep.msess.gov.pt/pesquisa_avancada.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

V.David
  • 75
  • 12
  • check this - http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource – Mike B Oct 31 '16 at 13:04

1 Answers1

3

Per your edit, you aren't allowed to client-side load content from other domains for security reasons.

If you want that content injected into your page, you should load it server side and include it with the response.

ryachza
  • 4,460
  • 18
  • 28
  • I already managed to show the full webpage with . is it possible to just show a div from that object? – V.David Oct 31 '16 at 13:11
  • @V.David I believe the issue there is that client-side you would not be allowed to script against the content inside an `object` or `iframe` etc., again for security reasons. The content you are loading would have to provide an API. – ryachza Oct 31 '16 at 13:14
  • @V.David Are you currently running any server-side logic? You should be able to execute a web request for the resource and then extract the target `div` and inject it in your page. Or set up an end-point on your server that simply passes that resource through (a proxy of sorts) and then the `.load` will be accessing your domain. – ryachza Oct 31 '16 at 13:21
  • No im not, im using wordpress. But im just trying to make some tests with a normal html file. (Sorry if i said anything stupid, kinda newb) – V.David Oct 31 '16 at 13:22
  • @V.David Really all good - everyone is new at some point, and this is an exceptionally common wall. If you're using WordPress, you should be able to use PHP to achieve what I described. You won't be able to do it with pure JavaScript without cooperation from the resource you're trying to consume. – ryachza Oct 31 '16 at 13:25
  • Alright! I will try to find a solution uising php! If i managed i will answer here! – V.David Oct 31 '16 at 13:33
  • @V.David This was the quickest resource I could find: http://htmlparsing.com/php.html. While you could probably brute force what you're trying to do using regular expressions, learning to use a DOM/parsing library would probably serve you well. curl is pretty ubiquitous. – ryachza Oct 31 '16 at 13:44