iFrames can be a convinent way of loading content from one domain onto another, but they do have their limitations. For example, it usually isn't possible to style the contents of the iFrame and you are therefore left at the mercy of a third party site. They also look pretty shonky if the third party site does down for whatever reason. Displaying large "page not found" statements on your page is quite unsightly.
There is a function in jQuery called load() that will use an AJAX request to load content from page onto another, and can even extract specific areas of the page and return only those parts. I thought I would run through some examples and then show how it is possible to display content from another domain on a page. Lets say we have a PHP file on the server that generates a random number, this would be the following very simple code.
<?php
echo rand();
This can be loaded into a div on a page of HTML using a single call in jQuery that utilises the load() function. This will just grab the text from the random_number.php file (the contents of which is in the above example) and put the result in the div with the id of test. Here is a page of HTML that does just that. Note that if you want to run this you'll need to put it on a server to enable the AJAX calls, otherwise it won't do anything.
<!DOCTYPE HTML>
<html>
<head>
<title>Loader</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#test').load('/random_number.php', '', function(response, status, xhr) {
if (status == 'error') {
var msg = "Sorry but there was an error: ";
$(".content").html(msg + xhr.status + " " + xhr.statusText);
}
});
});
</script>
</head>
<body>
<div id="test"></div>
</body>
</html>
Taking this a step further it is possible to get part of the text on a page by passing an extra bit of text to the load() function. The first parameter of the function is the location of the resource, but it is also possible to add a CSS identifier to the resource that will bring back only the block you were looking for. Take the following little HTML snippet, which would be loaded into a file called html_snippet.html on the server.
<div>Some <strong class="aclass">content</strong></div>
It is possible to grab the contents of just the "strong" element by adding a space and then the CSS selector you need to extract that bit of content, in this case it is ".aclass". This means we only need to change the jQuery example above a little bit to get this working.
$('#test').load('/html_snippet.html .aclass', '', function(response, status, xhr) {
The thing that separates iFrames from AJAX calls is that an iFrames can point anywhere, whereas an AJAX call MUST be on the same domain as the original file. This is a built in security feature of JavaScript and cannot be circumvented. This means we need to add a quick workaround to bypass this security mechanism. To take a realworld example I thought it might be a good idea to try and download some Google search results and print them onto the page. First we need to create the PHP code to download the Google search results, which is actually quite easy. Here we are searching for the term "wibble".
<?php
$url = 'http://www.google.com/search?hl=en&q=';
$output = file_get_contents($url . urlencode('wibble'));
echo $output;
Call this file something like searchgoogle.php and pop it on your server. We now need to update the jQuery so that we only display the search results and not all of the other stuff on the Google search results page that isn't needed. We do this by supplying the CSS selector "#res". Here is the updated line of code that you need to change. Once this is done the page automatically loads the test div with the search results for the term "wibble".
$('#test').load('/searchgoogle.php #res', '', function(response, status, xhr) {
Just loading the results onto the page when the DOM has loaded isn't quite the best way to do things in every situation, it is also nice to add a "Loading" graphic and a swishy effect when the content is changed to make things nice and pretty. This is all controlled via a link element at the top of the page. This requires a little bit more jQuery code to be added so I have printed it out in full again with the additional actions added. We are essentially adding a click event to the link element so that it will run the load() function. There are some other action and animation functions here that will cause the content to show and hide at different times, but the basic functionality is still there. Now, when a user clicks on the "Load" link they are presented with the search results for the term "wibble".
<!DOCTYPE HTML>
<html>
<head>
<title>Loader</title>
<style type="text/css">
div#container {
width:500px;
height:500px;
overflow:auto;
}
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript">
var externalpagepart = '#res';
var loadinggif = '/loading.gif';
$(document).ready(function(){
// set up the click event
$('a.loader').live('click', function() {
var toLoad = '/searchgoogle.php ' + externalpagepart;
$('.content').slideUp('slow', loadContent);
$('#load').remove();
$('#waiting').append('<div id="load"><img src="' + loadinggif + '" alt="Loading" /></div>');
$('#load').fadeIn('normal');
function loadContent() {
$('.content').load(toLoad, '', function(response, status, xhr) {
if (status == 'error') {
var msg = "Sorry but there was an error: ";
$(".content").html(msg + xhr.status + " " + xhr.statusText);
}
}).slideDown('slow', hideLoader());
}
function hideLoader() {
$('#load').fadeOut('normal');
}
return false;
});
});
</script>
</head>
<body>
<p><a class="loader" href="javascript: null(0);">Load</a></p>
<div id="container">
<p id="waiting"></p>
<div class="content"></div>
</div>
</body>
</html>
Finally, I thought it might be a good idea to integrate a input box into the page so that the user can control the search term they want to send over to Google. The first task is to do is to allow the passing of a post variable called 'string' to the Google search URL. This is wrapped in a call to urlencode() to stop any errors occurring when users type in spaces and other things. We will come onto how to send this data in the next snippet.
<?php
$url = 'http://www.google.com/search?hl=en&q=';
$postVariable = 'string';
if (isset($_POST['string'])) {
$output = file_get_contents($url . urlencode($_POST[$postVariable]));
$output = preg_replace('#<script.*</script>#ismU', '', $output);
$output = preg_replace('#<style.*</style>#ismU', '', $output);
echo $output;
}
To pass post values through the AJAX call we need to use the second parameter in the load() function. This is a JSON encoded collection of variables that we might want to use. The property of the JSON object is the key in the $_POST array on the PHP side. To send the value "wibble" to the previous PHP code snippet we use the following:
$('.content').load(toLoad, {'string': 'wibble'}, function(response, status, xhr) {
We can now tie this all together and create a form that will allow users to search Google without actually leaving the current page. To use this just type a value into the input box and click on the link. This will trigget the action and run the load() method. This can probably be tied into a form properly, but this is enough to demonstrate how things would work.
<!DOCTYPE HTML>
<html>
<head>
<title>Loader</title>
<style type="text/css">
div#container {
width:500px;
height:500px;
overflow:auto;
}
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript">
var externalpagepart = '#res';
var loadinggif = '/loading.gif';
$(document).ready(function(){
// set up the click event
$('a.loader').live('click', function() {
var toLoad = '/searchgoogle.php ' + externalpagepart;
$('.content').slideUp('slow', loadContent);
$('#load').remove();
$('#waiting').append('<div id="load"><img src="' + loadinggif + '" alt="Loading" /></div>');
$('#load').fadeIn('normal');
function loadContent() {
var searchterm = $('#searchterm').val();
$('.content').load(toLoad, {'string': searchterm}, function(response, status, xhr) {
if (status == 'error') {
var msg = "Sorry but there was an error: ";
$(".content").html(msg + xhr.status + " " + xhr.statusText);
}
}).slideDown('slow', hideLoader());
}
function hideLoader() {
$('#load').fadeOut('normal');
}
return false;
});
});
</script>
</head>
<body>
<input type="text" name="searchterm" id="searchterm" value="wibble" />
<p><a class="loader" href="javascript: null(0);">Load</a></p>
<div id="container">
<p id="waiting"></p>
<div class="content"></div>
</div>
</body>
</html>
One final thing that might be a good idea to do is strip out any style and script tags within the content of the page, this can be done through two calls to preg_replace(). This is probably a prudent thing to do anyway as you can end up with unwanted CSS and potentially dangerous JavaScript appearing on your site.
$url = 'http://www.google.com/search?hl=en&q=';
$postVariable = 'string';
if (isset($_POST['string'])) {
$output = file_get_contents($url . urlencode($_POST[$postVariable]));
$output = preg_replace('#<script.*</script>#ismU', '', $output);
$output = preg_replace('#<style.*</style>#ismU', '', $output);
echo $output;
}
This was al quite easy to set up and shows the power that a simple load() call can have, especially within a CMS application. Feel free to use these examples on your own sites but drop me a message to let me know as I would quite like to see other people's implementation of this method.
Comments
Hello. Thanks for the post, but I have a problem with the ajax feature with jquery.
It doesn't work in my website if it haven't the 'www.' before the name.
Example. www.mysite.com -> it works fine the ajax
mysite.com -> it doesn't work the ajax.
Does somebody know why??
Thanks!!
<a href="http://www.letter-example.com">Letter samples
</a>
Submitted by Miki on Mon, 02/14/2011 - 12:02
PermalinkYes, the problem is because you are trying to violate the JavaScript same origin policy, meaning that you can't run JavaScript between one domain (or subdomain) and another. This table sums up things quite nicely.
Submitted by giHlZp8M8D on Mon, 02/14/2011 - 12:26
PermalinkThank you for the explanation!
I have done some changes in my htaccess file, to redirect when the users don't put the 'www'
Thank you again.
Letter samples
Submitted by Miki on Mon, 02/14/2011 - 13:10
PermalinkGood article, I was hoping to use this to replace an iFrame. The page in question uses an iFrame to load a page from another website, however I only need the main content of the page loaded and not the top navigation bar.
Will the jQuery load() function allow me to do this?
Thanks,
John
Submitted by AnonymousJohn on Wed, 02/23/2011 - 03:42
PermalinkCan I use this method to strip the navigation form a page I'm loading using?
Submitted by AnonymousJohn on Wed, 02/23/2011 - 04:09
PermalinkThanks John! What your asking should be possible, as long as the content is contains within a selectable alea.
Submitted by giHlZp8M8D on Wed, 02/23/2011 - 08:45
PermalinkDo you just have a demonstratin using simple navigation links that display different content (paragraphs) on one page, no tabs, no php, just using Jquery (links only)?
Submitted by Anonymous on Tue, 09/13/2011 - 06:14
PermalinkFinally something that works. I think I've googled all the net these days and haven't found a script to load an external page in a div on the same page - yours does the trick.
Thank you very much!
Submitted by Portal Romanesc on Tue, 09/27/2011 - 12:21
PermalinkHi thanks for this great article. Very very nice. One thing - I'm currently loading some aspx code into an IFRAME on a joomla (php) site - the site and webapp are on the same domain.
CMS: http://mydomain.com/index.php
IFRAME on front page: http://mydomain.com/webapp/default.aspx
Would the above approach handle this? Or should I walk away now? :)
Thanks in advance.
Submitted by John Lewis on Thu, 12/01/2011 - 10:52
PermalinkTheoretically, it should work as you state. An iframe will allow you to load content from virtually any resource. If your aspx code is generating HTML then it will get shown in your iframe.
The only point I would like to raise is that the main point of this article was to load content onto a page without an iframe ;). However, the code above should work in the same way. I think...
Submitted by giHlZp8M8D on Thu, 12/01/2011 - 11:01
PermalinkHi, This example what you explained is using AJAX in a Server. But I want to do the same functionality without using Server and Iframes.
Is it Possible? If yes, How ?
Submitted by Naveen Kumar on Thu, 12/08/2011 - 05:35
PermalinkHi Naveen. This technique will work with any server side technology as it's a client side script that does all of the work really. You can replace this JavaScript calls with iframes, but the whole point of the article was to not use them.
Submitted by giHlZp8M8D on Thu, 12/08/2011 - 09:11
PermalinkI am looking for a way to show taged content from a blog. The idea is to let the client update his blog. Then on the web page the viewer clicks on the link "surfboards for sale" and each post on the blog that is taged "surfboard" shows up. . . . . Or am I compleatly off track here?
Submitted by Jesse Willson on Fri, 12/23/2011 - 06:21
PermalinkYes. I will work as long as you can call the correct URL and bring through the posts that way. I have no idea how blogger works though.
Submitted by giHlZp8M8D on Fri, 12/23/2011 - 10:24
PermalinkI'm writing a crawler, and I need to load web pages into a temporary div element, using jQuery.load() method. However, using such patterns as 'url-of-the-page body' won't get the body section of the result. I think, jQuery only can embed permissable elements in parent elements. In other words, you can't load an entire HTML document, into a div element.
Submitted by Saeed Neamati on Sat, 01/14/2012 - 07:37
PermalinkHi and thank you for the great information!
I was hoping to use this to replace an iFrame that I am using in a wordpress page also. The iframe is very slow and from all of my research I didn't think that you could do this. But I believe that it's what I need. I would love to able to add a few changes to the CSS of the ajaxed page also!
Would someone please show me an example of how to load my external page into WP without having to use the iframe? I am not a JS developer like most of you but I do love my CSS. Any information would be greatly appreciated!
Thanks again,
Brent
Submitted by Brent on Thu, 01/19/2012 - 20:51
PermalinkHi,
I am new to html and am trying to accomplish the following.
I have 12 html Pages (January through December) they are just well formed tables.
I want to load them into a div based on the user selection in a drop down.
Can this be done?
As I said, I am new so please be gentle.
Gary
Submitted by Gary Lee on Wed, 02/08/2012 - 07:25
PermalinkSubmitted by giHlZp8M8D on Wed, 02/08/2012 - 09:32
PermalinkThank you very much for sharing. Definitely solved my problem.
Submitted by Alex John on Fri, 03/09/2012 - 02:25
PermalinkHi,
Can I use it as login from?
if yes how?
Thanks
Submitted by Max on Fri, 03/09/2012 - 16:38
PermalinkHi,
Thanks for this wonderful post.... congratulations....
I was trying to use this method, but i am missing the <script> code which contains the javascript from the url i am loading.
i googled and found out jquery filters that <script> code,
You are genious and must have some alternative to this , please let me know how to get that <script> code too...
Thanks in advance...
Submitted by vijay on Wed, 03/14/2012 - 14:01
Permalinkhi. i'm trying to load a file with jquery.
example: $('#list_products').load("products/list_products.php", { 'filtro': id, 'tipo' : 'search' });
in my personal computer the products is loading correctly. in the server i see that is not passing the variables (filtro and tipo)
can you help me please.
personal computer : windows 7
server : linux
Submitted by Anonymous on Sun, 03/25/2012 - 11:30
PermalinkCould you send me zip of source files. sorry im a noob. I just realy want to learn this.. if you have time feel free to take a look at my attempt.
http://www.mrjakewalter.com/load/another/next/
Thanks again for your post!
Submitted by jake on Thu, 04/19/2012 - 18:26
PermalinkThanks for the comment Jake. All of the source code for this is right here on the page the zip file wouldn't contain anything else.
Looking at your example I would say that the JavaScript appears to be fine, it is the PHP script that appears not to produce any output. Try just echoing a string and seeing if that appears before trying to do more complex things with Google.
Remember that the Google example uses a POST request and therefore requires the second script which 'calls' the PHP file with the correct POST variables set.
Submitted by giHlZp8M8D on Fri, 04/20/2012 - 09:15
PermalinkAlso remember that the URL "/random_number.php" is an absolute URL and so points to a file on the root of the domain. If you are having trouble getting Try chaning the PHP file location to e.g. "random_number.php" (without the first slash) to make it a relative reference.
Submitted by giHlZp8M8D on Fri, 04/20/2012 - 09:24
PermalinkHi Philip,
Its a nice article....Is ther any alternate for iframe to display iframe other website content as part of our page.
Thanks
Submitted by Selva on Mon, 04/30/2012 - 10:07
Permalinkhello thxx alot realy i'm like this effect thxx so much
Submitted by فلام on Sat, 05/05/2012 - 18:37
PermalinkSubmitted by Guy on Tue, 05/08/2012 - 05:55
PermalinkSubmitted by Anthony on Tue, 05/15/2012 - 10:09
PermalinkSubmitted by giHlZp8M8D on Tue, 05/15/2012 - 10:28
PermalinkSubmitted by Anthony on Wed, 05/16/2012 - 10:22
PermalinkSubmitted by ifayce on Tue, 06/12/2012 - 12:51
PermalinkSubmitted by Pat on Thu, 06/14/2012 - 19:33
PermalinkCould you please help me
Submitted by MSK on Fri, 06/29/2012 - 19:31
PermalinkSubmitted by Morris Davis on Mon, 07/30/2012 - 12:53
PermalinkI have a span in a form frmTest: Form in C# as below:
New
When the page loads, the span shows the value as "New" ( Type : New) I want to reload this span every 2 secs to get its value from the server as it keeps changing(diff possible values are New, Modify, Delete). I have been trying the following but no luck. Can you please help me with this?
My code:
setInterval(function(){ $('#types').load('frmTest.cs #types'); },2000);
Submitted by jue on Wed, 08/15/2012 - 17:23
PermalinkSubmitted by Marcel on Sat, 09/08/2012 - 11:05
PermalinkIn the page have a text box and button.
The first time i press button, it work well, but i press the button again then error happen.
The error is:
Sys.WebForms.PageRequestManagerServerErrorException: The state information is invalid for this page and might be corrupted.
Submitted by Em on Mon, 10/08/2012 - 11:13
PermalinkI think...
Submitted by giHlZp8M8D on Mon, 10/08/2012 - 11:21
PermalinkSubmitted by Manish on Fri, 10/12/2012 - 10:53
PermalinkSubmitted by giHlZp8M8D on Fri, 10/12/2012 - 11:01
PermalinkHi, Thanks for the post !
Can you tell me how to use jQuery in the loaded content, please? Is it possible? Because for me it doesn't work yet. Is there a possibility to communicate in jQuery between container and content?
Phil
Submitted by Phil06 on Fri, 11/09/2012 - 00:05
PermalinkSubmitted by sanjeev on Wed, 02/13/2013 - 08:23
PermalinkSubmitted by carrie on Fri, 06/14/2013 - 18:37
PermalinkSubmitted by Andrew Sam on Fri, 08/22/2014 - 11:53
PermalinkSubmitted by Gary on Wed, 11/19/2014 - 12:21
PermalinkSubmitted by Anime Subways on Sun, 03/29/2015 - 03:18
PermalinkSubmitted by giHlZp8M8D on Mon, 03/30/2015 - 16:56
PermalinkSubmitted by Josip on Wed, 03/02/2016 - 10:07
PermalinkSubmitted by jessica on Wed, 01/04/2017 - 13:31
PermalinkAdd new comment