jQuery load external site page

You’re running into a cross domain policy issue cause AJAX (for security reasons) will not let you grab content from a page that does not sit on the same domain.

To get rid of it and accomplish your task:
you need a PHP file you can call grabber.php with just this line of PHP:

<?php echo file_get_contents($_GET['url']); ?>

Than inside your html (or whatever file just do like:)

<!DOCTYPE html>
<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <meta charset=utf-8 />
    <title>test</title>
</head>
<body>

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

</body>

<script>
$(function(){
    var contentURI= 'http://domain.com #element';    // URL TO GRAB + # of any desired element // if needed :)
    $('#response').load('grabber.php?url="+ contentURI);
});
</script>

</html>

Why does this work?

  • now, AJAX is sending a simple GET request to the grabber.php page,
  • grabber.php echoes the desired content
  • now the content is on your (server) domain!
  • and AJAX is happy to serve you 🙂

Leave a Comment